2 * Copyright (c) 2018 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
35 * @brief Wrapper for using wait queue as a kobject.
38 #include <synch/syswaitq.h>
39 #include <synch/waitq.h>
43 #include <proc/task.h>
44 #include <syscall/copy.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
,
65 cap_free(cap
->task
, cap
->handle
);
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
,
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.
90 sys_errno_t
sys_waitq_create(cap_waitq_handle_t
*whandle
)
92 waitq_t
*wq
= slab_alloc(waitq_cache
, FRAME_ATOMIC
);
94 return (sys_errno_t
) ENOMEM
;
97 kobject_t
*kobj
= kobject_alloc(0);
99 slab_free(waitq_cache
, wq
);
100 return (sys_errno_t
) ENOMEM
;
102 kobject_initialize(kobj
, KOBJECT_TYPE_WAITQ
, wq
, &waitq_kobject_ops
);
105 errno_t rc
= cap_alloc(TASK
, &handle
);
107 slab_free(waitq_cache
, wq
);
109 return (sys_errno_t
) rc
;
112 rc
= copy_to_uspace(whandle
, &handle
, sizeof(handle
));
114 cap_free(TASK
, handle
);
116 slab_free(waitq_cache
, wq
);
117 return (sys_errno_t
) rc
;
120 cap_publish(TASK
, handle
, kobj
);
122 return (sys_errno_t
) EOK
;
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
);
135 return (sys_errno_t
) ENOENT
;
137 cap_free(TASK
, whandle
);
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
148 * @return Error code.
150 sys_errno_t
sys_waitq_sleep(cap_waitq_handle_t whandle
, uint32_t timeout
,
153 kobject_t
*kobj
= kobject_get(TASK
, whandle
, KOBJECT_TYPE_WAITQ
);
155 return (sys_errno_t
) ENOENT
;
158 udebug_stoppable_begin();
161 errno_t rc
= waitq_sleep_timeout(kobj
->waitq
, timeout
,
162 SYNCH_FLAGS_INTERRUPTIBLE
| flags
, NULL
);
165 udebug_stoppable_end();
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
);
183 return (sys_errno_t
) ENOENT
;
185 waitq_wakeup(kobj
->waitq
, WAKEUP_FIRST
);
188 return (sys_errno_t
) EOK
;