source: mainline/kernel/generic/src/synch/syswaitq.c@ 455241b

Last change on this file since 455241b was 455241b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 5 months ago

Integrate kobject_t into target structures

This just means fewer allocations and indirections.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup kernel_sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Wrapper for using wait queue as a kobject.
36 */
37
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>
45
46#include <stdint.h>
47
48static slab_cache_t *waitq_cache;
49
50typedef struct {
51 kobject_t kobject;
52 waitq_t waitq;
53} waitq_kobject_t;
54
55static void waitq_destroy(kobject_t *arg)
56{
57 waitq_kobject_t *wq = (waitq_kobject_t *) arg;
58 slab_free(waitq_cache, wq);
59}
60
61kobject_ops_t waitq_kobject_ops = {
62 .destroy = waitq_destroy,
63};
64
65/** Initialize the user waitq subsystem */
66void sys_waitq_init(void)
67{
68 waitq_cache = slab_cache_create("syswaitq_t", sizeof(waitq_kobject_t),
69 0, NULL, NULL, 0);
70}
71
72/** Create a waitq for the current task
73 *
74 * @param[out] whandle Userspace address of the destination buffer that will
75 * receive the allocated waitq capability.
76 *
77 * @return Error code.
78 */
79sys_errno_t sys_waitq_create(uspace_ptr_cap_waitq_handle_t whandle)
80{
81 waitq_kobject_t *kobj = slab_alloc(waitq_cache, FRAME_ATOMIC);
82 if (!kobj)
83 return (sys_errno_t) ENOMEM;
84
85 kobject_initialize(&kobj->kobject, KOBJECT_TYPE_WAITQ);
86 waitq_initialize(&kobj->waitq);
87
88 cap_handle_t handle;
89 errno_t rc = cap_alloc(TASK, &handle);
90 if (rc != EOK) {
91 slab_free(waitq_cache, kobj);
92 return (sys_errno_t) rc;
93 }
94
95 rc = copy_to_uspace(whandle, &handle, sizeof(handle));
96 if (rc != EOK) {
97 cap_free(TASK, handle);
98 slab_free(waitq_cache, kobj);
99 return (sys_errno_t) rc;
100 }
101
102 cap_publish(TASK, handle, &kobj->kobject);
103
104 return (sys_errno_t) EOK;
105}
106
107/** Destroy a waitq
108 *
109 * @param whandle Waitq capability handle of the waitq to be destroyed.
110 *
111 * @return Error code.
112 */
113sys_errno_t sys_waitq_destroy(cap_waitq_handle_t whandle)
114{
115 kobject_t *kobj = cap_unpublish(TASK, whandle, KOBJECT_TYPE_WAITQ);
116 if (!kobj)
117 return (sys_errno_t) ENOENT;
118 kobject_put(kobj);
119 cap_free(TASK, whandle);
120 return EOK;
121}
122
123/** Sleep in the waitq
124 *
125 * @param whandle Waitq capability handle of the waitq in which to sleep.
126 * @param timeout Timeout in microseconds.
127 * @param flags Flags from SYNCH_FLAGS_* family. SYNCH_FLAGS_INTERRUPTIBLE is
128 * always implied.
129 *
130 * @return Error code.
131 */
132sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uint32_t timeout,
133 unsigned int flags)
134{
135 waitq_kobject_t *kobj =
136 (waitq_kobject_t *) kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
137 if (!kobj)
138 return (sys_errno_t) ENOENT;
139
140#ifdef CONFIG_UDEBUG
141 udebug_stoppable_begin();
142#endif
143
144 errno_t rc = _waitq_sleep_timeout(&kobj->waitq, timeout,
145 SYNCH_FLAGS_INTERRUPTIBLE | flags);
146
147#ifdef CONFIG_UDEBUG
148 udebug_stoppable_end();
149#endif
150
151 kobject_put(&kobj->kobject);
152
153 return (sys_errno_t) rc;
154}
155
156/** Wakeup a thread sleeping in the waitq
157 *
158 * @param whandle Waitq capability handle of the waitq to invoke wakeup on.
159 *
160 * @return Error code.
161 */
162sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
163{
164 waitq_kobject_t *kobj =
165 (waitq_kobject_t *) kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
166 if (!kobj)
167 return (sys_errno_t) ENOENT;
168
169 waitq_wake_one(&kobj->waitq);
170
171 kobject_put(&kobj->kobject);
172 return (sys_errno_t) EOK;
173}
174
175/** @}
176 */
Note: See TracBrowser for help on using the repository browser.