File: player_thread.c

package info (click to toggle)
gmerlin 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,108 kB
  • sloc: ansic: 133,761; javascript: 6,967; makefile: 1,400; sh: 702; sed: 16
file content (318 lines) | stat: -rw-r--r-- 7,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



#include <gmerlin/bgthread.h>

#include <pthread.h>

#include <gmerlin/log.h>
#define LOG_DOMAIN "player.thread"

#include <stdlib.h>
#include <stdio.h>

/* Binary semaphores: Like POSIX semaphores but the
   value can only be 1 or 0 */

typedef struct
  {
  int count;
  int nwaiting;
  pthread_mutex_t lock;
  pthread_cond_t cond;
  } bin_sem_t;

static void bin_sem_init(bin_sem_t * s)
  {
  pthread_mutex_init(&s->lock, NULL);
  pthread_cond_init(&s->cond, NULL);
  }

static void bin_sem_destroy(bin_sem_t * s)
  {
  pthread_mutex_destroy(&s->lock);
  pthread_cond_destroy(&s->cond);
  }

static void bin_sem_wait(bin_sem_t * s)
  {
  pthread_mutex_lock(&s->lock);
  if(!s->count)
    {
    s->nwaiting++;
    pthread_cond_wait(&s->cond, &s->lock);
    s->nwaiting--;
    }
  s->count = 0;
  pthread_mutex_unlock(&s->lock);
  }

static void bin_sem_post(bin_sem_t * s)
  {
  pthread_mutex_lock(&s->lock);

  s->count = 1;
  if(s->nwaiting)
    pthread_cond_broadcast(&s->cond);
  pthread_mutex_unlock(&s->lock);
  }

static void bin_sem_reset(bin_sem_t * s)
  {
  pthread_mutex_lock(&s->lock);
  s->count = 0;
  pthread_mutex_unlock(&s->lock);
  }

struct bg_thread_common_s
  {
  pthread_cond_t start_cond;
  pthread_mutex_t start_mutex;
  };

struct bg_thread_s
  {
  bg_thread_common_t * com;
  
  pthread_t thread;
  bin_sem_t sem;

  void * (*func)(void*);
  void * arg;
  
  int do_stop;
  int do_pause;
  pthread_mutex_t mutex;
  
  };


bg_thread_common_t * bg_thread_common_create()
  {
  bg_thread_common_t * com = calloc(1, sizeof(*com));
  pthread_cond_init(&com->start_cond, NULL);
  pthread_mutex_init(&com->start_mutex, NULL);
  return com;
  }

void bg_thread_common_destroy(bg_thread_common_t * com)
  {
  pthread_cond_destroy(&com->start_cond);
  pthread_mutex_destroy(&com->start_mutex);
  free(com);
  }

bg_thread_t *
bg_thread_create(bg_thread_common_t * com)
  {
  bg_thread_t * th = calloc(1, sizeof(*th));
  th->com = com;
  bin_sem_init(&th->sem);
  pthread_mutex_init(&th->mutex, NULL);
  return th;
  }

void bg_thread_destroy(bg_thread_t * th)
  {
  bin_sem_destroy(&th->sem);
  pthread_mutex_destroy(&th->mutex);
  free(th);
  }

void bg_thread_set_func(bg_thread_t * th,
                        void * (*func)(void*), void * arg)
  {
  th->func = func;
  th->arg = arg;
  th->do_pause = 0;
  th->do_stop = 0;
  }

void bg_threads_init(bg_thread_t ** th, int num)
  {
  int i;

  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      // fprintf(stderr, "Starting thread...\n");
      pthread_create(&th[i]->thread, NULL, th[i]->func, th[i]->arg);
      // fprintf(stderr, "Starting thread done\n");
      }
    }
  /* Wait until all threads are started */
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      // fprintf(stderr, "Sem wait...");
      bin_sem_wait(&th[i]->sem);
      // fprintf(stderr, "done ret: %d, val: %d\n", ret, val);
      }
    }
  
  }

void bg_threads_start(bg_thread_t ** th, int num)
  {
  int i;

  //  fprintf(stderr, "bg_threads_start\n");

  /* Lock the global mutex. This will succeed after all
     threads wait for the start condition */

  if(!num)
    return;
  
  pthread_mutex_lock(&th[0]->com->start_mutex);
  pthread_cond_broadcast(&th[0]->com->start_cond);
  pthread_mutex_unlock(&th[0]->com->start_mutex);

  /* Wait until all threads woke up */
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      bin_sem_wait(&th[i]->sem);
      // fprintf(stderr, "done ret: %d, val: %d\n", ret, val);
      }
    }

  //  fprintf(stderr, "bg_threads_start done\n");
  }

void bg_threads_pause(bg_thread_t ** th, int num)
  {
  int i;
  //  fprintf(stderr, "bg_threads_pause\n");

  /* Set pause flag */
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      pthread_mutex_lock(&th[i]->mutex);
      th[i]->do_pause = 1;
      pthread_mutex_unlock(&th[i]->mutex);
      }
    }
  
  /* Wait until all threads are paused */
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      bin_sem_wait(&th[i]->sem);
    }
  //  fprintf(stderr, "bg_threads_pause done\n");
  }

void bg_threads_join(bg_thread_t ** th, int num)
  {
  int i;
  /* Set stop flag */
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      pthread_mutex_lock(&th[i]->mutex);
      th[i]->do_stop = 1;
      pthread_mutex_unlock(&th[i]->mutex);
      }
    }

  /* Start the threads if they where paused.
     If not paused, this call does no harm */
  //  fprintf(stderr, "bg_threads_start %d\n", getpid());
  bg_threads_start(th, num);
  //  fprintf(stderr, "bg_threads_start done %d\n", getpid());
  
  for(i = 0; i < num; i++)
    {
    if(th[i]->func)
      {
      // fprintf(stderr, "Joining thread...\n");
      pthread_join(th[i]->thread, NULL);
      // fprintf(stderr, "Joining thread done, sem\n");

      bin_sem_reset(&th[i]->sem);
      //      bin_sem_init(&th[i]->sem, 0, 0);
      
      }
    }
  }
/* called from within the thread */

int bg_thread_wait_for_start(bg_thread_t * th)
  {
  int ret = 1;
  pthread_mutex_lock(&th->com->start_mutex);
  // fprintf(stderr, "Sem post...\n");
  bin_sem_post(&th->sem);
  // fprintf(stderr, "Sem post done\n");
  
  pthread_cond_wait(&th->com->start_cond, &th->com->start_mutex);
  pthread_mutex_unlock(&th->com->start_mutex);

  pthread_mutex_lock(&th->mutex);
  th->do_pause = 0;
  if(th->do_stop)
    ret = 0;
  
  pthread_mutex_unlock(&th->mutex);
  bin_sem_post(&th->sem);
  return ret;
  }

void bg_thread_exit(bg_thread_t * th)
  {
  /* bg_threads_start() (called by bg_threads_join())
     needs this */
  
  bin_sem_post(&th->sem);
  }

int bg_thread_check(bg_thread_t * th)
  {
  int do_pause;
  
  pthread_mutex_lock(&th->mutex);
  if(th->do_stop)
    {
    pthread_mutex_unlock(&th->mutex);
    bin_sem_post(&th->sem);
    return 0;
    }
  do_pause = th->do_pause;
  pthread_mutex_unlock(&th->mutex);
  
  if(do_pause)
    {
    pthread_mutex_lock(&th->mutex);
    th->do_pause = 0;
    pthread_mutex_unlock(&th->mutex);
    
    return bg_thread_wait_for_start(th);
    }
  return 1;
  }