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 *****************************************************************************/
28 #include <sys/types.h>
30 #include <sys/syscall.h>
31 #include <linux/futex.h>
33 #ifndef FUTEX_PRIVATE_FLAG
34 #define FUTEX_WAKE_PRIVATE FUTEX_WAKE
35 #define FUTEX_WAIT_PRIVATE FUTEX_WAIT
38 #include <vlc_common.h>
40 unsigned long vlc_thread_id(void)
42 static __thread pid_t tid
= 0;
44 if (unlikely(tid
== 0))
45 tid
= syscall(__NR_gettid
);
50 static int sys_futex(void *addr
, int op
, unsigned val
,
51 const struct timespec
*to
, void *addr2
, int val3
)
53 return syscall(__NR_futex
, addr
, op
, val
, to
, addr2
, val3
);
56 static int vlc_futex_wake(void *addr
, int nr
)
58 return sys_futex(addr
, FUTEX_WAKE_PRIVATE
, nr
, NULL
, NULL
, 0);
61 static int vlc_futex_wait(void *addr
, unsigned val
, const struct timespec
*to
)
63 return sys_futex(addr
, FUTEX_WAIT_PRIVATE
, val
, to
, NULL
, 0);
66 void vlc_addr_signal(void *addr
)
68 vlc_futex_wake(addr
, 1);
71 void vlc_addr_broadcast(void *addr
)
73 vlc_futex_wake(addr
, INT_MAX
);
76 void vlc_addr_wait(void *addr
, unsigned val
)
78 vlc_futex_wait(addr
, val
, NULL
);
81 bool vlc_addr_timedwait(void *addr
, unsigned val
, mtime_t delay
)
83 lldiv_t d
= lldiv(delay
, CLOCK_FREQ
);
84 struct timespec ts
= { d
.quot
, d
.rem
* (1000000000 / CLOCK_FREQ
) };
86 return (vlc_futex_wait(addr
, val
, &ts
) == 0 || errno
!= ETIMEDOUT
);