File: sem.c

package info (click to toggle)
binkd 1.1a-99-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,452 kB
  • sloc: ansic: 22,949; makefile: 943; perl: 369; sh: 325
file content (41 lines) | stat: -rw-r--r-- 1,011 bytes parent folder | download | duplicates (4)
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
/*
 *  sem.c -- binkd's posix semaphore wrapper
 *
 *  sem.c is a part of binkd project
 *
 *  Copyright (C) 2014-2014  Pavel Gulchuk and others
 *
 *  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. See COPYING.
 */

#include <time.h>
#include "../sys.h"
#include "../readcfg.h"
#include "../tools.h"
#include "../sem.h"

int _WaitSem(void *vpSem, int timeout) {
  EVENTSEM *sem = vpSem;
  struct timespec ts;
  int rc;

  pthread_mutex_lock(&(sem->mutex));
#ifdef HAVE_CLOCK_GETTIME
  clock_gettime(CLOCK_REALTIME, &ts);
#else
  {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    ts.tv_sec = tv.tv_sec + 0;
    ts.tv_nsec = tv.tv_usec * 1000;
  }
#endif
  ts.tv_sec += timeout;
  rc = pthread_cond_timedwait(&(sem->cond), &(sem->mutex), &ts);
  pthread_mutex_unlock(&(sem->mutex));
  return rc;
}