1/**
2 * \file
3 * \brief Barrelfish libc lock backend. These functions are used in
4 *        lib/newlib/newlib/libc/include/sys/lock.h
5 */
6
7/*
8 * Copyright (c) 2014, ETH Zurich.
9 * All rights reserved.
10 *
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
14 * Attn: Systems Group.
15 */
16
17#include <sys/lock.h>
18#include <assert.h>
19#include <barrelfish/barrelfish.h>
20#include <barrelfish/curdispatcher_arch.h>
21#include <barrelfish/threads.h>
22
23static void make_work_disabled(void)
24{
25    dispatcher_handle_t handle = curdispatcher();
26    struct dispatcher_shared_generic* disp =
27        get_dispatcher_shared_generic(handle);
28    if (disp->disabled) {
29        disp->disabled = 0;
30    }
31}
32
33void bf_libc_lock_init(struct thread_mutex *lock)
34{
35    thread_mutex_init(lock);
36}
37
38void bf_libc_lock_close(struct thread_mutex *lock)
39{ }
40
41void bf_libc_lock_acquire(struct thread_mutex *lock)
42{
43    make_work_disabled();
44    thread_mutex_lock(lock);
45}
46
47void bf_libc_lock_acquire_recursive(struct thread_mutex *lock)
48{
49    make_work_disabled();
50    thread_mutex_lock_nested(lock);
51}
52
53void bf_libc_lock_try_acquire(struct thread_mutex *lock)
54{
55    make_work_disabled();
56    thread_mutex_trylock(lock);
57}
58
59void bf_libc_lock_try_acquire_recursive(struct thread_mutex *lock)
60{
61    // can't do try_acquire_recursive
62    assert(!"NYI");
63}
64
65void bf_libc_lock_release(struct thread_mutex *lock)
66{
67    dispatcher_handle_t handle = curdispatcher();
68    struct dispatcher_shared_generic* disp =
69        get_dispatcher_shared_generic(handle);
70    if (disp->disabled) {
71        thread_mutex_unlock_disabled(curdispatcher(), lock);
72    } else {
73        thread_mutex_unlock(lock);
74    }
75}
76