1 /*****************************************************************************
2 * linux/thread.c: Linux specifics for threading
3 *****************************************************************************
4 * Copyright (C) 2016 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
29 #include <sys/types.h>
31 #include <sys/syscall.h>
32 #include <linux/futex.h>
34 #ifndef FUTEX_PRIVATE_FLAG
35 #define FUTEX_WAKE_PRIVATE FUTEX_WAKE
36 #define FUTEX_WAIT_PRIVATE FUTEX_WAIT
37 #define FUTEX_WAIT_BITSET_PRIVATE FUTEX_WAIT_BITSET
40 #include <vlc_common.h>
42 unsigned long vlc_thread_id(void)
44 static __thread pid_t tid
= 0;
46 if (unlikely(tid
== 0))
47 tid
= syscall(__NR_gettid
);
52 static int sys_futex(void *addr
, int op
, unsigned val
,
53 const struct timespec
*to
, void *addr2
, int val3
)
55 return syscall(__NR_futex
, addr
, op
, val
, to
, addr2
, val3
);
58 static int vlc_futex_wake(void *addr
, int nr
)
60 return sys_futex(addr
, FUTEX_WAKE_PRIVATE
, nr
, NULL
, NULL
, 0);
63 static int vlc_futex_wait(void *addr
, unsigned flags
,
64 unsigned val
, const struct timespec
*to
)
66 return sys_futex(addr
, FUTEX_WAIT_BITSET_PRIVATE
| flags
, val
, to
, NULL
,
67 FUTEX_BITSET_MATCH_ANY
);
70 void vlc_atomic_notify_one(void *addr
)
72 vlc_futex_wake(addr
, 1);
75 void vlc_atomic_notify_all(void *addr
)
77 vlc_futex_wake(addr
, INT_MAX
);
80 void vlc_atomic_wait(void *addr
, unsigned val
)
82 vlc_futex_wait(addr
, 0, val
, NULL
);
85 int vlc_atomic_timedwait(void *addr
, unsigned val
, vlc_tick_t deadline
)
87 struct timespec ts
= timespec_from_vlc_tick(deadline
);
89 if (vlc_futex_wait(addr
, 0, val
, &ts
) == 0)
98 vlc_assert_unreachable(); /* BUG! */
105 int vlc_atomic_timedwait_daytime(void *addr
, unsigned val
, time_t deadline
)
107 struct timespec ts
= { .tv_sec
= deadline
, .tv_nsec
= 0 };
109 if (vlc_futex_wait(addr
, FUTEX_CLOCK_REALTIME
, val
, &ts
) == 0)
118 vlc_assert_unreachable(); /* BUG! */