You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
16 KiB

10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*======
  5. This file is part of PerconaFT.
  6. Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
  7. PerconaFT is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License, version 2,
  9. as published by the Free Software Foundation.
  10. PerconaFT is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
  16. ----------------------------------------
  17. PerconaFT is free software: you can redistribute it and/or modify
  18. it under the terms of the GNU Affero General Public License, version 3,
  19. as published by the Free Software Foundation.
  20. PerconaFT is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU Affero General Public License for more details.
  24. You should have received a copy of the GNU Affero General Public License
  25. along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
  26. ======= */
  27. #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
  28. #pragma once
  29. #include <pthread.h>
  30. #include <time.h>
  31. #include <stdint.h>
  32. #include "toku_portability.h"
  33. #include "toku_assert.h"
  34. // TODO: some things moved toku_instrumentation.h, not necessarily the best
  35. // place
  36. typedef pthread_attr_t toku_pthread_attr_t;
  37. typedef pthread_t toku_pthread_t;
  38. typedef pthread_mutex_t toku_pthread_mutex_t;
  39. typedef pthread_condattr_t toku_pthread_condattr_t;
  40. typedef pthread_cond_t toku_pthread_cond_t;
  41. typedef pthread_rwlockattr_t toku_pthread_rwlockattr_t;
  42. typedef pthread_key_t toku_pthread_key_t;
  43. typedef struct timespec toku_timespec_t;
  44. // TODO: break this include loop
  45. #include <pthread.h>
  46. typedef pthread_mutexattr_t toku_pthread_mutexattr_t;
  47. struct toku_mutex_t {
  48. pthread_mutex_t pmutex;
  49. struct PSI_mutex
  50. *psi_mutex; /* The performance schema instrumentation hook */
  51. #if TOKU_PTHREAD_DEBUG
  52. pthread_t owner; // = pthread_self(); // for debugging
  53. bool locked;
  54. bool valid;
  55. pfs_key_t instr_key_id;
  56. #endif
  57. };
  58. struct toku_cond_t {
  59. pthread_cond_t pcond;
  60. struct PSI_cond *psi_cond;
  61. #if TOKU_PTHREAD_DEBUG
  62. pfs_key_t instr_key_id;
  63. #endif
  64. };
  65. #ifdef TOKU_PTHREAD_DEBUG
  66. #define TOKU_COND_INITIALIZER \
  67. { \
  68. .pcond = PTHREAD_COND_INITIALIZER, .psi_cond = nullptr, \
  69. .instr_key_id = 0 \
  70. }
  71. #else
  72. #define TOKU_COND_INITIALIZER \
  73. { .pcond = PTHREAD_COND_INITIALIZER, .psi_cond = nullptr }
  74. #endif
  75. struct toku_pthread_rwlock_t {
  76. pthread_rwlock_t rwlock;
  77. struct PSI_rwlock *psi_rwlock;
  78. #if TOKU_PTHREAD_DEBUG
  79. pfs_key_t instr_key_id;
  80. #endif
  81. };
  82. typedef struct toku_mutex_aligned {
  83. toku_mutex_t aligned_mutex __attribute__((__aligned__(64)));
  84. } toku_mutex_aligned_t;
  85. // Initializing with {} will fill in a struct with all zeros.
  86. // But you may also need a pragma to suppress the warnings, as follows
  87. //
  88. // #pragma GCC diagnostic push
  89. // #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  90. // toku_mutex_t foo = ZERO_MUTEX_INITIALIZER;
  91. // #pragma GCC diagnostic pop
  92. //
  93. // In general it will be a lot of busy work to make this codebase compile
  94. // cleanly with -Wmissing-field-initializers
  95. #define ZERO_MUTEX_INITIALIZER \
  96. {}
  97. #if TOKU_PTHREAD_DEBUG
  98. #define TOKU_MUTEX_INITIALIZER \
  99. { \
  100. .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr, .owner = 0, \
  101. .locked = false, .valid = true, .instr_key_id = 0 \
  102. }
  103. #else
  104. #define TOKU_MUTEX_INITIALIZER \
  105. { .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr }
  106. #endif
  107. // Darwin doesn't provide adaptive mutexes
  108. #if defined(__APPLE__)
  109. #define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_DEFAULT
  110. #if TOKU_PTHREAD_DEBUG
  111. #define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
  112. { \
  113. .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr, .owner = 0, \
  114. .locked = false, .valid = true, .instr_key_id = 0 \
  115. }
  116. #else
  117. #define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
  118. { .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr }
  119. #endif
  120. #else // __FreeBSD__, __linux__, at least
  121. #define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_ADAPTIVE_NP
  122. #if TOKU_PTHREAD_DEBUG
  123. #define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
  124. { \
  125. .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, .psi_mutex = nullptr, \
  126. .owner = 0, .locked = false, .valid = true, .instr_key_id = 0 \
  127. }
  128. #else
  129. #define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
  130. { .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, .psi_mutex = nullptr }
  131. #endif
  132. #endif
  133. // Different OSes implement mutexes as different amounts of nested structs.
  134. // C++ will fill out all missing values with zeroes if you provide at least one
  135. // zero, but it needs the right amount of nesting.
  136. #if defined(__FreeBSD__)
  137. #define ZERO_COND_INITIALIZER \
  138. { 0 }
  139. #elif defined(__APPLE__)
  140. #define ZERO_COND_INITIALIZER \
  141. { \
  142. { 0 } \
  143. }
  144. #else // __linux__, at least
  145. #define ZERO_COND_INITIALIZER \
  146. {}
  147. #endif
  148. static inline void toku_mutexattr_init(toku_pthread_mutexattr_t *attr) {
  149. int r = pthread_mutexattr_init(attr);
  150. assert_zero(r);
  151. }
  152. static inline void
  153. toku_mutexattr_settype(toku_pthread_mutexattr_t *attr, int type) {
  154. int r = pthread_mutexattr_settype(attr, type);
  155. assert_zero(r);
  156. }
  157. static inline void
  158. toku_mutexattr_destroy(toku_pthread_mutexattr_t *attr) {
  159. int r = pthread_mutexattr_destroy(attr);
  160. assert_zero(r);
  161. }
  162. #if TOKU_PTHREAD_DEBUG
  163. static inline void toku_mutex_assert_locked(const toku_mutex_t *mutex) {
  164. invariant(mutex->locked);
  165. invariant(mutex->owner == pthread_self());
  166. }
  167. #else
  168. static inline void
  169. toku_mutex_assert_locked(const toku_mutex_t *mutex __attribute__((unused))) {
  170. }
  171. #endif
  172. // asserting that a mutex is unlocked only makes sense
  173. // if the calling thread can guaruntee that no other threads
  174. // are trying to lock this mutex at the time of the assertion
  175. //
  176. // a good example of this is a tree with mutexes on each node.
  177. // when a node is locked the caller knows that no other threads
  178. // can be trying to lock its childrens' mutexes. the children
  179. // are in one of two fixed states: locked or unlocked.
  180. #if TOKU_PTHREAD_DEBUG
  181. static inline void
  182. toku_mutex_assert_unlocked(toku_mutex_t *mutex) {
  183. invariant(mutex->owner == 0);
  184. invariant(!mutex->locked);
  185. }
  186. #else
  187. static inline void toku_mutex_assert_unlocked(toku_mutex_t *mutex
  188. __attribute__((unused))) {}
  189. #endif
  190. #define toku_mutex_lock(M) \
  191. toku_mutex_lock_with_source_location(M, __FILE__, __LINE__)
  192. static inline void toku_cond_init(toku_cond_t *cond,
  193. const toku_pthread_condattr_t *attr) {
  194. int r = pthread_cond_init(&cond->pcond, attr);
  195. assert_zero(r);
  196. }
  197. #define toku_mutex_trylock(M) \
  198. toku_mutex_trylock_with_source_location(M, __FILE__, __LINE__)
  199. inline void toku_mutex_unlock(toku_mutex_t *mutex) {
  200. #if TOKU_PTHREAD_DEBUG
  201. invariant(mutex->owner == pthread_self());
  202. invariant(mutex->valid);
  203. invariant(mutex->locked);
  204. mutex->locked = false;
  205. mutex->owner = 0;
  206. #endif
  207. toku_instr_mutex_unlock(mutex->psi_mutex);
  208. int r = pthread_mutex_unlock(&mutex->pmutex);
  209. assert_zero(r);
  210. }
  211. inline void toku_mutex_lock_with_source_location(toku_mutex_t *mutex,
  212. const char *src_file,
  213. int src_line) {
  214. toku_mutex_instrumentation mutex_instr;
  215. toku_instr_mutex_lock_start(mutex_instr, *mutex, src_file, src_line);
  216. const int r = pthread_mutex_lock(&mutex->pmutex);
  217. toku_instr_mutex_lock_end(mutex_instr, r);
  218. assert_zero(r);
  219. #if TOKU_PTHREAD_DEBUG
  220. invariant(mutex->valid);
  221. invariant(!mutex->locked);
  222. invariant(mutex->owner == 0);
  223. mutex->locked = true;
  224. mutex->owner = pthread_self();
  225. #endif
  226. }
  227. inline int toku_mutex_trylock_with_source_location(toku_mutex_t *mutex,
  228. const char *src_file,
  229. int src_line) {
  230. toku_mutex_instrumentation mutex_instr;
  231. toku_instr_mutex_trylock_start(mutex_instr, *mutex, src_file, src_line);
  232. const int r = pthread_mutex_lock(&mutex->pmutex);
  233. toku_instr_mutex_lock_end(mutex_instr, r);
  234. #if TOKU_PTHREAD_DEBUG
  235. if (r == 0) {
  236. invariant(mutex->valid);
  237. invariant(!mutex->locked);
  238. invariant(mutex->owner == 0);
  239. mutex->locked = true;
  240. mutex->owner = pthread_self();
  241. }
  242. #endif
  243. return r;
  244. }
  245. #define toku_cond_wait(C, M) \
  246. toku_cond_wait_with_source_location(C, M, __FILE__, __LINE__)
  247. #define toku_cond_timedwait(C, M, W) \
  248. toku_cond_timedwait_with_source_location(C, M, W, __FILE__, __LINE__)
  249. inline void toku_cond_init(const toku_instr_key &key,
  250. toku_cond_t *cond,
  251. const pthread_condattr_t *attr) {
  252. toku_instr_cond_init(key, *cond);
  253. int r = pthread_cond_init(&cond->pcond, attr);
  254. assert_zero(r);
  255. }
  256. inline void toku_cond_destroy(toku_cond_t *cond) {
  257. toku_instr_cond_destroy(cond->psi_cond);
  258. int r = pthread_cond_destroy(&cond->pcond);
  259. assert_zero(r);
  260. }
  261. inline void toku_cond_wait_with_source_location(toku_cond_t *cond,
  262. toku_mutex_t *mutex,
  263. const char *src_file,
  264. uint src_line) {
  265. #if TOKU_PTHREAD_DEBUG
  266. invariant(mutex->locked);
  267. mutex->locked = false;
  268. mutex->owner = 0;
  269. #endif
  270. /* Instrumentation start */
  271. toku_cond_instrumentation cond_instr;
  272. toku_instr_cond_wait_start(cond_instr,
  273. toku_instr_cond_op::cond_wait,
  274. *cond,
  275. *mutex,
  276. src_file,
  277. src_line);
  278. /* Instrumented code */
  279. const int r = pthread_cond_wait(&cond->pcond, &mutex->pmutex);
  280. /* Instrumentation end */
  281. toku_instr_cond_wait_end(cond_instr, r);
  282. assert_zero(r);
  283. #if TOKU_PTHREAD_DEBUG
  284. invariant(!mutex->locked);
  285. mutex->locked = true;
  286. mutex->owner = pthread_self();
  287. #endif
  288. }
  289. inline int toku_cond_timedwait_with_source_location(toku_cond_t *cond,
  290. toku_mutex_t *mutex,
  291. toku_timespec_t *wakeup_at,
  292. const char *src_file,
  293. uint src_line) {
  294. #if TOKU_PTHREAD_DEBUG
  295. invariant(mutex->locked);
  296. mutex->locked = false;
  297. mutex->owner = 0;
  298. #endif
  299. /* Instrumentation start */
  300. toku_cond_instrumentation cond_instr;
  301. toku_instr_cond_wait_start(cond_instr,
  302. toku_instr_cond_op::cond_timedwait,
  303. *cond,
  304. *mutex,
  305. src_file,
  306. src_line);
  307. /* Instrumented code */
  308. const int r = pthread_cond_timedwait(
  309. &cond->pcond, &mutex->pmutex, wakeup_at);
  310. /* Instrumentation end */
  311. toku_instr_cond_wait_end(cond_instr, r);
  312. #if TOKU_PTHREAD_DEBUG
  313. invariant(!mutex->locked);
  314. mutex->locked = true;
  315. mutex->owner = pthread_self();
  316. #endif
  317. return r;
  318. }
  319. inline void toku_cond_signal(toku_cond_t *cond) {
  320. toku_instr_cond_signal(*cond);
  321. const int r = pthread_cond_signal(&cond->pcond);
  322. assert_zero(r);
  323. }
  324. inline void toku_cond_broadcast(toku_cond_t *cond) {
  325. toku_instr_cond_broadcast(*cond);
  326. const int r = pthread_cond_broadcast(&cond->pcond);
  327. assert_zero(r);
  328. }
  329. inline void toku_mutex_init(const toku_instr_key &key,
  330. toku_mutex_t *mutex,
  331. const toku_pthread_mutexattr_t *attr) {
  332. #if TOKU_PTHREAD_DEBUG
  333. mutex->valid = true;
  334. #endif
  335. toku_instr_mutex_init(key, *mutex);
  336. const int r = pthread_mutex_init(&mutex->pmutex, attr);
  337. assert_zero(r);
  338. #if TOKU_PTHREAD_DEBUG
  339. mutex->locked = false;
  340. invariant(mutex->valid);
  341. mutex->valid = true;
  342. mutex->owner = 0;
  343. #endif
  344. }
  345. inline void toku_mutex_destroy(toku_mutex_t *mutex) {
  346. #if TOKU_PTHREAD_DEBUG
  347. invariant(mutex->valid);
  348. mutex->valid = false;
  349. invariant(!mutex->locked);
  350. #endif
  351. toku_instr_mutex_destroy(mutex->psi_mutex);
  352. int r = pthread_mutex_destroy(&mutex->pmutex);
  353. assert_zero(r);
  354. }
  355. #define toku_pthread_rwlock_rdlock(RW) \
  356. toku_pthread_rwlock_rdlock_with_source_location(RW, __FILE__, __LINE__)
  357. #define toku_pthread_rwlock_wrlock(RW) \
  358. toku_pthread_rwlock_wrlock_with_source_location(RW, __FILE__, __LINE__)
  359. inline void toku_pthread_rwlock_init(
  360. const toku_instr_key &key,
  361. toku_pthread_rwlock_t *__restrict rwlock,
  362. const toku_pthread_rwlockattr_t *__restrict attr) {
  363. toku_instr_rwlock_init(key, *rwlock);
  364. int r = pthread_rwlock_init(&rwlock->rwlock, attr);
  365. assert_zero(r);
  366. }
  367. inline void toku_pthread_rwlock_destroy(toku_pthread_rwlock_t *rwlock) {
  368. toku_instr_rwlock_destroy(rwlock->psi_rwlock);
  369. int r = pthread_rwlock_destroy(&rwlock->rwlock);
  370. assert_zero(r);
  371. }
  372. inline void toku_pthread_rwlock_rdlock_with_source_location(
  373. toku_pthread_rwlock_t *rwlock,
  374. const char *src_file,
  375. uint src_line) {
  376. /* Instrumentation start */
  377. toku_rwlock_instrumentation rwlock_instr;
  378. toku_instr_rwlock_rdlock_wait_start(
  379. rwlock_instr, *rwlock, src_file, src_line);
  380. /* Instrumented code */
  381. const int r = pthread_rwlock_rdlock(&rwlock->rwlock);
  382. /* Instrumentation end */
  383. toku_instr_rwlock_rdlock_wait_end(rwlock_instr, r);
  384. assert_zero(r);
  385. }
  386. inline void toku_pthread_rwlock_wrlock_with_source_location(
  387. toku_pthread_rwlock_t *rwlock,
  388. const char *src_file,
  389. uint src_line) {
  390. /* Instrumentation start */
  391. toku_rwlock_instrumentation rwlock_instr;
  392. toku_instr_rwlock_wrlock_wait_start(
  393. rwlock_instr, *rwlock, src_file, src_line);
  394. /* Instrumented code */
  395. const int r = pthread_rwlock_wrlock(&rwlock->rwlock);
  396. /* Instrumentation end */
  397. toku_instr_rwlock_wrlock_wait_end(rwlock_instr, r);
  398. assert_zero(r);
  399. }
  400. inline void toku_pthread_rwlock_rdunlock(toku_pthread_rwlock_t *rwlock) {
  401. toku_instr_rwlock_unlock(*rwlock);
  402. const int r = pthread_rwlock_unlock(&rwlock->rwlock);
  403. assert_zero(r);
  404. }
  405. inline void toku_pthread_rwlock_wrunlock(toku_pthread_rwlock_t *rwlock) {
  406. toku_instr_rwlock_unlock(*rwlock);
  407. const int r = pthread_rwlock_unlock(&rwlock->rwlock);
  408. assert_zero(r);
  409. }
  410. static inline int toku_pthread_join(toku_pthread_t thread, void **value_ptr) {
  411. return pthread_join(thread, value_ptr);
  412. }
  413. static inline int
  414. toku_pthread_detach(toku_pthread_t thread) {
  415. return pthread_detach(thread);
  416. }
  417. static inline int
  418. toku_pthread_key_create(toku_pthread_key_t *key, void (*destroyf)(void *)) {
  419. return pthread_key_create(key, destroyf);
  420. }
  421. static inline int
  422. toku_pthread_key_delete(toku_pthread_key_t key) {
  423. return pthread_key_delete(key);
  424. }
  425. static inline void *
  426. toku_pthread_getspecific(toku_pthread_key_t key) {
  427. return pthread_getspecific(key);
  428. }
  429. static inline int toku_pthread_setspecific(toku_pthread_key_t key, void *data) {
  430. return pthread_setspecific(key, data);
  431. }
  432. int toku_pthread_yield(void) __attribute__((__visibility__("default")));
  433. static inline toku_pthread_t toku_pthread_self(void) { return pthread_self(); }
  434. static inline void *toku_pthread_done(void *exit_value) {
  435. toku_instr_delete_current_thread();
  436. pthread_exit(exit_value);
  437. }