[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 |
|
---|
| 50 | static void waitq_destroy(void *arg)
|
---|
| 51 | {
|
---|
| 52 | waitq_t *wq = (waitq_t *) arg;
|
---|
| 53 | slab_free(waitq_cache, wq);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | static kobject_ops_t waitq_kobject_ops = {
|
---|
| 57 | .destroy = waitq_destroy
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | static bool waitq_cap_cleanup_cb(cap_t *cap, void *arg)
|
---|
| 61 | {
|
---|
| 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;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /** Initialize the user waitq subsystem */
|
---|
| 70 | void sys_waitq_init(void)
|
---|
| 71 | {
|
---|
| 72 | waitq_cache = slab_cache_create("waitq_t", sizeof(waitq_t), 0, NULL,
|
---|
| 73 | NULL, 0);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Clean-up all waitq capabilities held by the exiting task */
|
---|
| 77 | void sys_waitq_task_cleanup(void)
|
---|
| 78 | {
|
---|
| 79 | caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_WAITQ,
|
---|
| 80 | waitq_cap_cleanup_cb, NULL);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Create a waitq for the current task
|
---|
| 84 | *
|
---|
| 85 | * @param[out] whandle Userspace address of the destination buffer that will
|
---|
| 86 | * receive the allocated waitq capability.
|
---|
| 87 | *
|
---|
| 88 | * @return Error code.
|
---|
| 89 | */
|
---|
| 90 | sys_errno_t sys_waitq_create(cap_waitq_handle_t *whandle)
|
---|
| 91 | {
|
---|
| 92 | waitq_t *wq = slab_alloc(waitq_cache, FRAME_ATOMIC);
|
---|
| 93 | if (!wq)
|
---|
| 94 | return (sys_errno_t) ENOMEM;
|
---|
| 95 | waitq_initialize(wq);
|
---|
| 96 |
|
---|
| 97 | kobject_t *kobj = (kobject_t *) malloc(sizeof(kobject_t));
|
---|
| 98 | if (!kobj) {
|
---|
| 99 | slab_free(waitq_cache, wq);
|
---|
| 100 | return (sys_errno_t) ENOMEM;
|
---|
| 101 | }
|
---|
| 102 | kobject_initialize(kobj, KOBJECT_TYPE_WAITQ, wq, &waitq_kobject_ops);
|
---|
| 103 |
|
---|
| 104 | cap_handle_t handle;
|
---|
| 105 | errno_t rc = cap_alloc(TASK, &handle);
|
---|
| 106 | if (rc != EOK) {
|
---|
| 107 | slab_free(waitq_cache, wq);
|
---|
| 108 | free(kobj);
|
---|
| 109 | return (sys_errno_t) rc;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | rc = copy_to_uspace(whandle, &handle, sizeof(handle));
|
---|
| 113 | if (rc != EOK) {
|
---|
| 114 | cap_free(TASK, handle);
|
---|
| 115 | free(kobj);
|
---|
| 116 | slab_free(waitq_cache, wq);
|
---|
| 117 | return (sys_errno_t) rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | cap_publish(TASK, handle, kobj);
|
---|
| 121 |
|
---|
| 122 | return (sys_errno_t) EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Sleep in the waitq
|
---|
| 126 | *
|
---|
| 127 | * @param whandle Waitq capability handle of the waitq in which to sleep.
|
---|
| 128 | * @param timeout Timeout in microseconds.
|
---|
| 129 | *
|
---|
| 130 | * @return Error code.
|
---|
| 131 | */
|
---|
| 132 | sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uintptr_t timeout)
|
---|
| 133 | {
|
---|
| 134 | kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
| 135 | if (!kobj)
|
---|
| 136 | return (sys_errno_t) ENOENT;
|
---|
| 137 |
|
---|
| 138 | #ifdef CONFIG_UDEBUG
|
---|
| 139 | udebug_stoppable_begin();
|
---|
| 140 | #endif
|
---|
| 141 |
|
---|
| 142 | errno_t rc = waitq_sleep_timeout(kobj->waitq, timeout,
|
---|
| 143 | SYNCH_FLAGS_INTERRUPTIBLE | SYNCH_FLAGS_FUTEX, NULL);
|
---|
| 144 |
|
---|
| 145 | #ifdef CONFIG_UDEBUG
|
---|
| 146 | udebug_stoppable_end();
|
---|
| 147 | #endif
|
---|
| 148 |
|
---|
| 149 | kobject_put(kobj);
|
---|
| 150 |
|
---|
| 151 | return (sys_errno_t) rc;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Wakeup a thread sleeping in the waitq
|
---|
| 155 | *
|
---|
| 156 | * @param whandle Waitq capability handle of the waitq to invoke wakeup on.
|
---|
| 157 | *
|
---|
| 158 | * @return Error code.
|
---|
| 159 | */
|
---|
| 160 | sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
|
---|
| 161 | {
|
---|
| 162 | kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
| 163 | if (!kobj)
|
---|
| 164 | return (sys_errno_t) ENOENT;
|
---|
| 165 |
|
---|
| 166 | waitq_wakeup(kobj->waitq, WAKEUP_FIRST);
|
---|
| 167 |
|
---|
| 168 | kobject_put(kobj);
|
---|
| 169 | return (sys_errno_t) EOK;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /** @}
|
---|
| 173 | */
|
---|