Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/futex.c

    ra35b458 r063a74b9  
    3535#include <futex.h>
    3636#include <atomic.h>
     37#include <libarch/barrier.h>
     38#include <libc.h>
     39#include <sys/types.h>
    3740
    3841/** Initialize futex counter.
     
    4447void futex_initialize(futex_t *futex, int val)
    4548{
    46         atomic_set(&futex->val, val);
     49        atomic_set(futex, val);
    4750}
    4851
     52/** Try to down the futex.
     53 *
     54 * @param futex Futex.
     55 *
     56 * @return Non-zero if the futex was acquired.
     57 * @return Zero if the futex was not acquired.
     58 *
     59 */
     60int futex_trydown(futex_t *futex)
     61{
     62        int rc;
    4963
    50 #ifdef FUTEX_UPGRADABLE
     64        rc = cas(futex, 1, 0);
     65        CS_ENTER_BARRIER();
    5166
    52 int _upgrade_futexes = 0;
    53 static futex_t upg_and_wait_futex = FUTEX_INITIALIZER;
    54 
    55 void futex_upgrade_all_and_wait(void)
    56 {
    57         futex_down(&upg_and_wait_futex);
    58 
    59         if (!_upgrade_futexes) {
    60                 rcu_assign(_upgrade_futexes, 1);
    61                 _rcu_synchronize(BM_BLOCK_THREAD);
    62         }
    63 
    64         futex_up(&upg_and_wait_futex);
     67        return rc;
    6568}
    6669
    67 #endif
     70/** Down the futex.
     71 *
     72 * @param futex Futex.
     73 *
     74 * @return ENOENT if there is no such virtual address.
     75 * @return Zero in the uncontended case.
     76 * @return Otherwise one of ESYNCH_OK_ATOMIC or ESYNCH_OK_BLOCKED.
     77 *
     78 */
     79int futex_down(futex_t *futex)
     80{
     81        atomic_signed_t nv;
     82
     83        nv = (atomic_signed_t) atomic_predec(futex);
     84        CS_ENTER_BARRIER();
     85        if (nv < 0)
     86                return __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->count);
     87       
     88        return 0;
     89}
     90
     91/** Up the futex.
     92 *
     93 * @param futex Futex.
     94 *
     95 * @return ENOENT if there is no such virtual address.
     96 * @return Zero in the uncontended case.
     97 *
     98 */
     99int futex_up(futex_t *futex)
     100{
     101        CS_LEAVE_BARRIER();
     102
     103        if ((atomic_signed_t) atomic_postinc(futex) < 0)
     104                return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->count);
     105       
     106        return 0;
     107}
    68108
    69109/** @}
Note: See TracChangeset for help on using the changeset viewer.