[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 |
|
---|
[fc0de8c] | 56 | kobject_ops_t waitq_kobject_ops = {
|
---|
[d314571] | 57 | .destroy = waitq_destroy
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | /** Initialize the user waitq subsystem */
|
---|
| 61 | void sys_waitq_init(void)
|
---|
| 62 | {
|
---|
| 63 | waitq_cache = slab_cache_create("waitq_t", sizeof(waitq_t), 0, NULL,
|
---|
| 64 | NULL, 0);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /** Create a waitq for the current task
|
---|
| 68 | *
|
---|
| 69 | * @param[out] whandle Userspace address of the destination buffer that will
|
---|
| 70 | * receive the allocated waitq capability.
|
---|
| 71 | *
|
---|
| 72 | * @return Error code.
|
---|
| 73 | */
|
---|
[5a5269d] | 74 | sys_errno_t sys_waitq_create(uspace_ptr_cap_waitq_handle_t whandle)
|
---|
[d314571] | 75 | {
|
---|
| 76 | waitq_t *wq = slab_alloc(waitq_cache, FRAME_ATOMIC);
|
---|
| 77 | if (!wq)
|
---|
| 78 | return (sys_errno_t) ENOMEM;
|
---|
| 79 | waitq_initialize(wq);
|
---|
| 80 |
|
---|
[e394c196] | 81 | kobject_t *kobj = kobject_alloc(0);
|
---|
[d314571] | 82 | if (!kobj) {
|
---|
| 83 | slab_free(waitq_cache, wq);
|
---|
| 84 | return (sys_errno_t) ENOMEM;
|
---|
| 85 | }
|
---|
[fc0de8c] | 86 | kobject_initialize(kobj, KOBJECT_TYPE_WAITQ, wq);
|
---|
[d314571] | 87 |
|
---|
| 88 | cap_handle_t handle;
|
---|
| 89 | errno_t rc = cap_alloc(TASK, &handle);
|
---|
| 90 | if (rc != EOK) {
|
---|
| 91 | slab_free(waitq_cache, wq);
|
---|
[e394c196] | 92 | kobject_free(kobj);
|
---|
[d314571] | 93 | return (sys_errno_t) rc;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | rc = copy_to_uspace(whandle, &handle, sizeof(handle));
|
---|
| 97 | if (rc != EOK) {
|
---|
| 98 | cap_free(TASK, handle);
|
---|
[e394c196] | 99 | kobject_free(kobj);
|
---|
[d314571] | 100 | slab_free(waitq_cache, wq);
|
---|
| 101 | return (sys_errno_t) rc;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | cap_publish(TASK, handle, kobj);
|
---|
| 105 |
|
---|
| 106 | return (sys_errno_t) EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[269bc459] | 109 | /** Destroy a waitq
|
---|
| 110 | *
|
---|
| 111 | * @param whandle Waitq capability handle of the waitq to be destroyed.
|
---|
| 112 | *
|
---|
| 113 | * @return Error code.
|
---|
| 114 | */
|
---|
| 115 | sys_errno_t sys_waitq_destroy(cap_waitq_handle_t whandle)
|
---|
| 116 | {
|
---|
| 117 | kobject_t *kobj = cap_unpublish(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
| 118 | if (!kobj)
|
---|
| 119 | return (sys_errno_t) ENOENT;
|
---|
| 120 | kobject_put(kobj);
|
---|
| 121 | cap_free(TASK, whandle);
|
---|
| 122 | return EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[d314571] | 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.
|
---|
[0b8fad2] | 129 | * @param flags Flags from SYNCH_FLAGS_* family. SYNCH_FLAGS_INTERRUPTIBLE is
|
---|
| 130 | * always implied.
|
---|
[d314571] | 131 | *
|
---|
| 132 | * @return Error code.
|
---|
| 133 | */
|
---|
[0b8fad2] | 134 | sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uint32_t timeout,
|
---|
| 135 | unsigned int flags)
|
---|
[d314571] | 136 | {
|
---|
| 137 | kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
| 138 | if (!kobj)
|
---|
| 139 | return (sys_errno_t) ENOENT;
|
---|
| 140 |
|
---|
| 141 | #ifdef CONFIG_UDEBUG
|
---|
| 142 | udebug_stoppable_begin();
|
---|
| 143 | #endif
|
---|
| 144 |
|
---|
[111b9b9] | 145 | errno_t rc = _waitq_sleep_timeout(kobj->waitq, timeout,
|
---|
| 146 | SYNCH_FLAGS_INTERRUPTIBLE | flags);
|
---|
[d314571] | 147 |
|
---|
| 148 | #ifdef CONFIG_UDEBUG
|
---|
| 149 | udebug_stoppable_end();
|
---|
| 150 | #endif
|
---|
| 151 |
|
---|
| 152 | kobject_put(kobj);
|
---|
| 153 |
|
---|
| 154 | return (sys_errno_t) rc;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** Wakeup a thread sleeping in the waitq
|
---|
| 158 | *
|
---|
| 159 | * @param whandle Waitq capability handle of the waitq to invoke wakeup on.
|
---|
| 160 | *
|
---|
| 161 | * @return Error code.
|
---|
| 162 | */
|
---|
| 163 | sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
|
---|
| 164 | {
|
---|
| 165 | kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
|
---|
| 166 | if (!kobj)
|
---|
| 167 | return (sys_errno_t) ENOENT;
|
---|
| 168 |
|
---|
[111b9b9] | 169 | waitq_wake_one(kobj->waitq);
|
---|
[d314571] | 170 |
|
---|
| 171 | kobject_put(kobj);
|
---|
| 172 | return (sys_errno_t) EOK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** @}
|
---|
| 176 | */
|
---|