osSemaphore.c 5.8 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
slguan 已提交
17 18
#define _DEFAULT_SOURCE
#include "os.h"
wafwerar's avatar
wafwerar 已提交
19
#include "pthread.h"
L
Liu Jicong 已提交
20
#include "tdef.h"
S
Shengliang Guan 已提交
21

wafwerar's avatar
wafwerar 已提交
22
#ifdef WINDOWS
S
Shengliang Guan 已提交
23 24 25 26 27 28 29

/*
 * windows implementation
 */

#include <windows.h>

wafwerar's avatar
wafwerar 已提交
30
bool taosCheckPthreadValid(TdThread thread) { return thread.p != NULL; }
S
Shengliang Guan 已提交
31

wafwerar's avatar
wafwerar 已提交
32
void taosResetPthread(TdThread* thread) { thread->p = 0; }
S
Shengliang Guan 已提交
33

wafwerar's avatar
wafwerar 已提交
34
int64_t taosGetPthreadId(TdThread thread) {
S
Shengliang Guan 已提交
35 36 37 38 39 40 41 42 43
#ifdef PTW32_VERSION
  return pthread_getw32threadid_np(thread);
#else
  return (int64_t)thread;
#endif
}

int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); }

wafwerar's avatar
wafwerar 已提交
44
bool taosComparePthread(TdThread first, TdThread second) { return first.p == second.p; }
S
Shengliang Guan 已提交
45 46 47

int32_t taosGetPId() { return GetCurrentProcessId(); }

48
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
49 50 51 52 53 54 55
  char filepath[1024] = {0};

  GetModuleFileName(NULL, filepath, MAX_PATH);
  char* sub = strrchr(filepath, '.');
  if (sub != NULL) {
    *sub = '\0';
  }
wafwerar's avatar
wafwerar 已提交
56 57 58 59 60
  char* end = strrchr(filepath, TD_DIRSEP[0]);
  if (end == NULL) {
    end = filepath;
  }

wafwerar's avatar
wafwerar 已提交
61
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
62 63

  if (len != NULL) {
wafwerar's avatar
wafwerar 已提交
64
    *len = (int32_t)strlen(end);
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

F
facetosea 已提交
78
int32_t tsem_timewait(tsem_t* sem, int64_t ms) {
L
Liu Jicong 已提交
79
  struct timespec ts;
F
facetosea 已提交
80 81
  taosClockGetTime(0, &ts);

L
Liu Jicong 已提交
82 83 84
  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;
F
facetosea 已提交
85 86 87
  int rc;
  while ((rc = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) continue;
  return rc;
wafwerar's avatar
wafwerar 已提交
88
  /* This should have timed out */
wafwerar's avatar
wafwerar 已提交
89 90 91 92 93 94
  // assert(errno == ETIMEDOUT);
  // assert(rc != 0);
  // GetSystemTimeAsFileTime(&ft_after);
  // // We specified a non-zero wait. Time must advance.
  // if (ft_before.dwLowDateTime == ft_after.dwLowDateTime && ft_before.dwHighDateTime == ft_after.dwHighDateTime)
  //   {
S
Shengliang Guan 已提交
95
  //     printf("nanoseconds: %d, rc: %d, code:0x%x. before filetime: %d, %d; after filetime: %d, %d\n",
wafwerar's avatar
wafwerar 已提交
96 97 98 99 100 101
  //         nanosecs, rc, errno,
  //         (int)ft_before.dwLowDateTime, (int)ft_before.dwHighDateTime,
  //         (int)ft_after.dwLowDateTime, (int)ft_after.dwHighDateTime);
  //     printf("time must advance during sem_timedwait.");
  //     return 1;
  //   }
wafwerar's avatar
wafwerar 已提交
102 103
}

S
Shengliang Guan 已提交
104 105
#elif defined(_TD_DARWIN_64)

F
freemine 已提交
106 107
#include <libproc.h>

L
Liu Jicong 已提交
108
int tsem_init(tsem_t *psem, int flags, unsigned int count) {
wafwerar's avatar
wafwerar 已提交
109 110
  *psem = dispatch_semaphore_create(count);
  if (*psem == NULL) return -1;
L
Liu Jicong 已提交
111
  return 0;
112 113
}

L
Liu Jicong 已提交
114
int tsem_destroy(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
115
  if (psem == NULL || *psem == NULL) return -1;
wafwerar's avatar
wafwerar 已提交
116 117
  // dispatch_release(*psem);
  // *psem = NULL;
L
Liu Jicong 已提交
118
  return 0;
119 120
}

L
Liu Jicong 已提交
121
int tsem_post(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
122 123
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_signal(*psem);
L
Liu Jicong 已提交
124
  return 0;
wafwerar's avatar
wafwerar 已提交
125
}
F
freemine 已提交
126

L
Liu Jicong 已提交
127
int tsem_wait(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
128 129
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER);
L
Liu Jicong 已提交
130
  return 0;
S
slguan 已提交
131
}
F
freemine 已提交
132

L
Liu Jicong 已提交
133
int tsem_timewait(tsem_t *psem, int64_t milis) {
wafwerar's avatar
wafwerar 已提交
134
  if (psem == NULL || *psem == NULL) return -1;
L
Liu Jicong 已提交
135
  dispatch_semaphore_wait(*psem, milis * 1000 * 1000);
L
Liu Jicong 已提交
136
  return 0;
F
freemine 已提交
137 138
}

wafwerar's avatar
wafwerar 已提交
139
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
wafwerar's avatar
wafwerar 已提交
140

F
freemine 已提交
141
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
142 143
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
144 145
}

wafwerar's avatar
wafwerar 已提交
146
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
F
freemine 已提交
147

wafwerar's avatar
wafwerar 已提交
148
void taosResetPthread(TdThread *thread) { *thread = NULL; }
F
freemine 已提交
149

wafwerar's avatar
wafwerar 已提交
150
bool taosComparePthread(TdThread first, TdThread second) { return taosThreadEqual(first, second) ? true : false; }
F
freemine 已提交
151

S
Shengliang Guan 已提交
152
int32_t taosGetPId() { return (int32_t)getpid(); }
F
freemine 已提交
153

154
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
155
  char buf[PATH_MAX + 1];
F
freemine 已提交
156
  buf[0] = '\0';
S
Shengliang Guan 已提交
157
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
158 159 160
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
wafwerar's avatar
wafwerar 已提交
161
  if (name) tstrncpy(name, buf, TSDB_APP_NAME_LEN);
F
freemine 已提交
162 163 164
  return 0;
}

S
Shengliang Guan 已提交
165 166 167 168 169 170 171
#else

/*
 * linux implementation
 */

#include <sys/syscall.h>
S
Shengliang Guan 已提交
172
#include <unistd.h>
S
Shengliang Guan 已提交
173

wafwerar's avatar
wafwerar 已提交
174
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
175 176 177 178 179 180 181 182

int64_t taosGetSelfPthreadId() {
  static __thread int id = 0;
  if (id != 0) return id;
  id = syscall(SYS_gettid);
  return id;
}

wafwerar's avatar
wafwerar 已提交
183 184 185
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
void    taosResetPthread(TdThread* thread) { *thread = 0; }
bool    taosComparePthread(TdThread first, TdThread second) { return first == second; }
L
Liu Jicong 已提交
186 187

int32_t taosGetPId() {
L
Liu Jicong 已提交
188
  static int32_t pid;
L
Liu Jicong 已提交
189 190 191 192
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
193

194
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
195 196
  const char* self = "/proc/self/exe";
  char        path[PATH_MAX] = {0};
F
freemine 已提交
197

S
Shengliang Guan 已提交
198 199 200 201 202 203 204 205 206 207 208 209
  if (readlink(self, path, PATH_MAX) <= 0) {
    return -1;
  }

  path[PATH_MAX - 1] = 0;
  char* end = strrchr(path, '/');
  if (end == NULL) {
    return -1;
  }

  ++end;

wafwerar's avatar
wafwerar 已提交
210
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  if (len != NULL) {
    *len = strlen(name);
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

L
Liu Jicong 已提交
227
int32_t tsem_timewait(tsem_t* sem, int64_t ms) {
L
Liu Jicong 已提交
228 229
  int ret = 0;

L
Liu Jicong 已提交
230 231 232 233 234 235 236 237 238
  struct timespec ts = {0};

  if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
    return -1;
  }

  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;
L
Liu Jicong 已提交
239

L
Liu Jicong 已提交
240
  while ((ret = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) continue;
L
Liu Jicong 已提交
241 242 243 244

  return ret;
}

S
Shengliang Guan 已提交
245
#endif