1 /*****************************************************************************
2 * freebsd/thread.c: FreeBSD specifics for atomic wait
3 *****************************************************************************
4 * Copyright (C) 2020 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>
34 #include <vlc_common.h>
36 unsigned long vlc_thread_id(void)
38 static _Thread_local
int tid
= -1;
40 if (unlikely(tid
== -1))
46 static int vlc_umtx_wake(void *addr
, int nr
)
48 return _umtx_op(addr
, UMTX_OP_WAKE_PRIVATE
, nr
, NULL
, NULL
);
51 static int vlc_umtx_wait(void *addr
, unsigned val
, const struct timespec
*ts
)
53 struct _umtx_time to
= {
55 ._flags
= UMTX_ABSTIME
,
56 ._clockid
= CLOCK_MONOTONIC
,
59 /* "the uaddr value [...] must be equal to the size of the structure
60 * pointed to by uaddr2, casted to uintptr_t."
62 void *uaddr
= (void *)sizeof (to
);
64 return _umtx_op(addr
, UMTX_OP_WAIT_UINT_PRIVATE
, val
, uaddr
, &to
);
67 void vlc_atomic_notify_one(void *addr
)
69 vlc_umtx_wake(addr
, 1);
72 void vlc_atomic_notify_all(void *addr
)
74 vlc_umtx_wake(addr
, INT_MAX
);
77 void vlc_atomic_wait(void *addr
, unsigned val
)
79 int ret
= vlc_umtx_wait(addr
, val
, NULL
);
81 assert(ret
== 0 || ret
== EINTR
|| ret
== ERESTART
);
84 int vlc_atomic_timedwait(void *addr
, unsigned val
, vlc_tick_t deadline
)
86 struct timespec ts
= timespec_from_vlc_tick(delay
);
87 int ret
= vlc_umtx_wait(addr
, val
, &ts
);
89 assert(ret
== 0 || ret
== ETIMEDOUT
|| ret
== EINTR
|| ret
== ERESTART
);
90 return (ret
!= ETIMEDOUT
) ? 0 : ret
;