Use a dedicated cache for instances of kobject_t
[helenos.git] / kernel / generic / src / synch / syswaitq.c
blob30e709f4d38c6b2b008ec2b522f5869f59b7d88a
1 /*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup kernel_sync
30 * @{
33 /**
34 * @file
35 * @brief Wrapper for using wait queue as a kobject.
38 #include <synch/syswaitq.h>
39 #include <synch/waitq.h>
40 #include <abi/cap.h>
41 #include <cap/cap.h>
42 #include <mm/slab.h>
43 #include <proc/task.h>
44 #include <syscall/copy.h>
46 #include <stdint.h>
48 static slab_cache_t *waitq_cache;
50 static void waitq_destroy(void *arg)
52 waitq_t *wq = (waitq_t *) arg;
53 slab_free(waitq_cache, wq);
56 static kobject_ops_t waitq_kobject_ops = {
57 .destroy = waitq_destroy
60 static bool waitq_cap_cleanup_cb(cap_t *cap, void *arg)
62 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
63 KOBJECT_TYPE_WAITQ);
64 kobject_put(kobj);
65 cap_free(cap->task, cap->handle);
66 return true;
69 /** Initialize the user waitq subsystem */
70 void sys_waitq_init(void)
72 waitq_cache = slab_cache_create("waitq_t", sizeof(waitq_t), 0, NULL,
73 NULL, 0);
76 /** Clean-up all waitq capabilities held by the exiting task */
77 void sys_waitq_task_cleanup(void)
79 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_WAITQ,
80 waitq_cap_cleanup_cb, NULL);
83 /** Create a waitq for the current task
85 * @param[out] whandle Userspace address of the destination buffer that will
86 * receive the allocated waitq capability.
88 * @return Error code.
90 sys_errno_t sys_waitq_create(cap_waitq_handle_t *whandle)
92 waitq_t *wq = slab_alloc(waitq_cache, FRAME_ATOMIC);
93 if (!wq)
94 return (sys_errno_t) ENOMEM;
95 waitq_initialize(wq);
97 kobject_t *kobj = kobject_alloc(0);
98 if (!kobj) {
99 slab_free(waitq_cache, wq);
100 return (sys_errno_t) ENOMEM;
102 kobject_initialize(kobj, KOBJECT_TYPE_WAITQ, wq, &waitq_kobject_ops);
104 cap_handle_t handle;
105 errno_t rc = cap_alloc(TASK, &handle);
106 if (rc != EOK) {
107 slab_free(waitq_cache, wq);
108 kobject_free(kobj);
109 return (sys_errno_t) rc;
112 rc = copy_to_uspace(whandle, &handle, sizeof(handle));
113 if (rc != EOK) {
114 cap_free(TASK, handle);
115 kobject_free(kobj);
116 slab_free(waitq_cache, wq);
117 return (sys_errno_t) rc;
120 cap_publish(TASK, handle, kobj);
122 return (sys_errno_t) EOK;
125 /** Destroy a waitq
127 * @param whandle Waitq capability handle of the waitq to be destroyed.
129 * @return Error code.
131 sys_errno_t sys_waitq_destroy(cap_waitq_handle_t whandle)
133 kobject_t *kobj = cap_unpublish(TASK, whandle, KOBJECT_TYPE_WAITQ);
134 if (!kobj)
135 return (sys_errno_t) ENOENT;
136 kobject_put(kobj);
137 cap_free(TASK, whandle);
138 return EOK;
141 /** Sleep in the waitq
143 * @param whandle Waitq capability handle of the waitq in which to sleep.
144 * @param timeout Timeout in microseconds.
145 * @param flags Flags from SYNCH_FLAGS_* family. SYNCH_FLAGS_INTERRUPTIBLE is
146 * always implied.
148 * @return Error code.
150 sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uint32_t timeout,
151 unsigned int flags)
153 kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
154 if (!kobj)
155 return (sys_errno_t) ENOENT;
157 #ifdef CONFIG_UDEBUG
158 udebug_stoppable_begin();
159 #endif
161 errno_t rc = waitq_sleep_timeout(kobj->waitq, timeout,
162 SYNCH_FLAGS_INTERRUPTIBLE | flags, NULL);
164 #ifdef CONFIG_UDEBUG
165 udebug_stoppable_end();
166 #endif
168 kobject_put(kobj);
170 return (sys_errno_t) rc;
173 /** Wakeup a thread sleeping in the waitq
175 * @param whandle Waitq capability handle of the waitq to invoke wakeup on.
177 * @return Error code.
179 sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
181 kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
182 if (!kobj)
183 return (sys_errno_t) ENOENT;
185 waitq_wakeup(kobj->waitq, WAKEUP_FIRST);
187 kobject_put(kobj);
188 return (sys_errno_t) EOK;
191 /** @}