[d314571] | 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 |
|
---|
| 48 | static slab_cache_t *waitq_cache;
|
---|
| 49 |
|
---|
[455241b] | 50 | typedef struct {
|
---|
| 51 | kobject_t kobject;
|
---|
| 52 | waitq_t waitq;
|
---|
| 53 | } waitq_kobject_t;
|
---|
| 54 |
|
---|
| 55 | static void waitq_destroy(kobject_t *arg)
|
---|
[d314571] | 56 | {
|
---|
[455241b] | 57 | waitq_kobject_t *wq = (waitq_kobject_t *) arg;
|
---|
[d314571] | 58 | slab_free(waitq_cache, wq);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[fc0de8c] | 61 | kobject_ops_t waitq_kobject_ops = {
|
---|
[455241b] | 62 | .destroy = waitq_destroy,
|
---|
[d314571] | 63 | };
|
---|
| 64 |
|
---|
| 65 | /** Initialize the user waitq subsystem */
|
---|
| 66 | void sys_waitq_init(void)
|
---|
| 67 | {
|
---|
[455241b] | 68 | waitq_cache = slab_cache_create("syswaitq_t", sizeof(waitq_kobject_t),
|
---|
| 69 | 0, NULL, NULL, 0);
|
---|
[d314571] | 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 | */
|
---|
[5a5269d] | 79 | sys_errno_t sys_waitq_create(uspace_ptr_cap_waitq_handle_t whandle)
|
---|
[d314571] | 80 | {
|
---|
[455241b] | 81 | waitq_kobject_t *kobj = slab_alloc(waitq_cache, FRAME_ATOMIC);
|
---|
| 82 | if (!kobj)
|
---|
[d314571] | 83 | return (sys_errno_t) ENOMEM;
|
---|
| 84 |
|
---|
[455241b] | 85 | kobject_initialize(&kobj->kobject, KOBJECT_TYPE_WAITQ);
|
---|
| 86 | waitq_initialize(&kobj->waitq);
|
---|
[d314571] | 87 |
|
---|
| 88 | cap_handle_t handle;
|
---|
| 89 | errno_t rc = cap_alloc(TASK, &handle);
|
---|
| 90 | if (rc != EOK) {
|
---|
[455241b] | 91 | slab_free(waitq_cache, kobj);
|
---|
[d314571] | 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);
|
---|
[455241b] | 98 | slab_free(waitq_cache, kobj);
|
---|
[d314571] | 99 | return (sys_errno_t) rc;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[455241b] | 102 | cap_publish(TASK, handle, &kobj->kobject);
|
---|
[d314571] | 103 |
|
---|
| 104 | return (sys_errno_t) EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[269bc459] | 107 | /** Destroy a waitq
|
---|
| 108 | *
|
---|
| 109 | * @param whandle Waitq capability handle of the waitq to be destroyed.
|
---|
| 110 | *
|
---|
| 111 | * @return Error code.
|
---|
| 112 | */
|
---|
| 113 | sys_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 |
|
---|
[d314571] | 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.
|
---|
[0b8fad2] | 127 | * @param flags Flags from SYNCH_FLAGS_* family. SYNCH_FLAGS_INTERRUPTIBLE is
|
---|
| 128 | * always implied.
|
---|
[d314571] | 129 | *
|
---|
| 130 | * @return Error code.
|
---|
| 131 | */
|
---|
[0b8fad2] | 132 | sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uint32_t timeout,
|
---|
| 133 | unsigned int flags)
|
---|
[d314571] | 134 | {
|
---|
[455241b] | 135 | waitq_kobject_t *kobj =
|
---|
| 136 | (waitq_kobject_t *) kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
[d314571] | 137 | if (!kobj)
|
---|
| 138 | return (sys_errno_t) ENOENT;
|
---|
| 139 |
|
---|
| 140 | #ifdef CONFIG_UDEBUG
|
---|
| 141 | udebug_stoppable_begin();
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
[455241b] | 144 | errno_t rc = _waitq_sleep_timeout(&kobj->waitq, timeout,
|
---|
[111b9b9] | 145 | SYNCH_FLAGS_INTERRUPTIBLE | flags);
|
---|
[d314571] | 146 |
|
---|
| 147 | #ifdef CONFIG_UDEBUG
|
---|
| 148 | udebug_stoppable_end();
|
---|
| 149 | #endif
|
---|
| 150 |
|
---|
[455241b] | 151 | kobject_put(&kobj->kobject);
|
---|
[d314571] | 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 | */
|
---|
| 162 | sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
|
---|
| 163 | {
|
---|
[455241b] | 164 | waitq_kobject_t *kobj =
|
---|
| 165 | (waitq_kobject_t *) kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
[d314571] | 166 | if (!kobj)
|
---|
| 167 | return (sys_errno_t) ENOENT;
|
---|
| 168 |
|
---|
[455241b] | 169 | waitq_wake_one(&kobj->waitq);
|
---|
[d314571] | 170 |
|
---|
[455241b] | 171 | kobject_put(&kobj->kobject);
|
---|
[d314571] | 172 | return (sys_errno_t) EOK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** @}
|
---|
| 176 | */
|
---|