Debian Codegraph @

src/rist-thread.c

raw symbols SCIP
1 /*
2 * Copyright © 2021, VideoLAN and librist authors
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "pthread-shim.h"
9 #include "rist-private.h"
10 #include "config.h"
11
12 #include <assert.h>
13
14 struct thread_wrapper {
15 struct rist_common_ctx *cctx;
16 pthread_start_func_t thread_func;
17 void *thread_arg;
18 };
19
20 /*
21 This is a bit of a hack, it allows us to do a callback to the calling application with the thread handle
22 when creating a thread, and when the wrapped function exits
23 */
24 static PTHREAD_START_FUNC(thread_wrapper, arg)
25 {
26 struct thread_wrapper *tw = arg;
27 void *handle = NULL;
28 #ifndef _WIN32
29 void *ret = NULL;
30 pthread_t p_id = pthread_self();
31 handle = &p_id;
32 #else //_WIN32
33 #if HAVE_PTHREADS
34 void *ret = NULL;
35 #else // HAVE_PTHREADS
36 DWORD ret = 0;
37 #endif // HAVE_PTHREADS
38 HANDLE h = GetCurrentThread();
39 handle = &h;
40 #endif //_WIN32
41 tw->cctx->thread_callback(handle, true, 0, tw->cctx->thread_callback_arg);
42 ret = tw->thread_func(tw->thread_arg);
43 tw->cctx->thread_callback(handle, false, 0, tw->cctx->thread_callback_arg);
44 free(arg);
45 return ret;
46 }
47
48 int rist_thread_create(struct rist_common_ctx *cctx,
49 pthread_t *thread, pthread_attr_t *attr, pthread_start_func_t thread_func, void *thread_arg)
50 {
51 if (cctx->thread_callback == NULL)
52 return pthread_create(thread, attr, thread_func, thread_arg);
53 struct thread_wrapper *tw = malloc(sizeof(*tw));
54 assert(tw != NULL);
55 tw->cctx = cctx;
56 tw->thread_func = thread_func;
57 tw->thread_arg = thread_arg;
58 int ret = pthread_create(thread, attr, thread_wrapper, tw);
59 if (ret != 0)
60 free(tw);
61 return ret;
62 }