util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2010 Jeff Garzik
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #define _GNU_SOURCE
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <jansson.h>
  18. #include <curl/curl.h>
  19. #include <time.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #ifndef WIN32
  24. # include <sys/socket.h>
  25. # include <netinet/in.h>
  26. # include <netinet/tcp.h>
  27. #else
  28. # include <winsock2.h>
  29. # include <mstcpip.h>
  30. #endif
  31. #include "miner.h"
  32. #include "elist.h"
  33. #include "compat.h"
  34. #if JANSSON_MAJOR_VERSION >= 2
  35. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  36. #else
  37. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  38. #endif
  39. bool successful_connect = false;
  40. struct timeval nettime;
  41. struct data_buffer {
  42. void *buf;
  43. size_t len;
  44. };
  45. struct upload_buffer {
  46. const void *buf;
  47. size_t len;
  48. };
  49. struct header_info {
  50. char *lp_path;
  51. bool has_rolltime;
  52. char *reason;
  53. };
  54. struct tq_ent {
  55. void *data;
  56. struct list_head q_node;
  57. };
  58. static void databuf_free(struct data_buffer *db)
  59. {
  60. if (!db)
  61. return;
  62. free(db->buf);
  63. memset(db, 0, sizeof(*db));
  64. }
  65. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  66. void *user_data)
  67. {
  68. struct data_buffer *db = user_data;
  69. size_t len = size * nmemb;
  70. size_t oldlen, newlen;
  71. void *newmem;
  72. static const unsigned char zero = 0;
  73. oldlen = db->len;
  74. newlen = oldlen + len;
  75. newmem = realloc(db->buf, newlen + 1);
  76. if (!newmem)
  77. return 0;
  78. db->buf = newmem;
  79. db->len = newlen;
  80. memcpy(db->buf + oldlen, ptr, len);
  81. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  82. return len;
  83. }
  84. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  85. void *user_data)
  86. {
  87. struct upload_buffer *ub = user_data;
  88. unsigned int len = size * nmemb;
  89. if (len > ub->len)
  90. len = ub->len;
  91. if (len) {
  92. memcpy(ptr, ub->buf, len);
  93. ub->buf += len;
  94. ub->len -= len;
  95. }
  96. return len;
  97. }
  98. static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
  99. {
  100. struct header_info *hi = user_data;
  101. size_t remlen, slen, ptrlen = size * nmemb;
  102. char *rem, *val = NULL, *key = NULL;
  103. void *tmp;
  104. val = calloc(1, ptrlen);
  105. key = calloc(1, ptrlen);
  106. if (!key || !val)
  107. goto out;
  108. tmp = memchr(ptr, ':', ptrlen);
  109. if (!tmp || (tmp == ptr)) /* skip empty keys / blanks */
  110. goto out;
  111. slen = tmp - ptr;
  112. if ((slen + 1) == ptrlen) /* skip key w/ no value */
  113. goto out;
  114. memcpy(key, ptr, slen); /* store & nul term key */
  115. key[slen] = 0;
  116. rem = ptr + slen + 1; /* trim value's leading whitespace */
  117. remlen = ptrlen - slen - 1;
  118. while ((remlen > 0) && (isspace(*rem))) {
  119. remlen--;
  120. rem++;
  121. }
  122. memcpy(val, rem, remlen); /* store value, trim trailing ws */
  123. val[remlen] = 0;
  124. while ((*val) && (isspace(val[strlen(val) - 1]))) {
  125. val[strlen(val) - 1] = 0;
  126. }
  127. if (!*val) /* skip blank value */
  128. goto out;
  129. if (opt_protocol)
  130. applog(LOG_DEBUG, "HTTP hdr(%s): %s", key, val);
  131. if (!strcasecmp("X-Roll-Ntime", key)) {
  132. if (!strncasecmp("N", val, 1)) {
  133. applog(LOG_DEBUG, "X-Roll-Ntime: N found");
  134. } else {
  135. applog(LOG_DEBUG, "X-Roll-Ntime found");
  136. hi->has_rolltime = true;
  137. }
  138. }
  139. if (!strcasecmp("X-Long-Polling", key)) {
  140. hi->lp_path = val; /* steal memory reference */
  141. val = NULL;
  142. }
  143. if (!strcasecmp("X-Reject-Reason", key)) {
  144. hi->reason = val; /* steal memory reference */
  145. val = NULL;
  146. }
  147. out:
  148. free(key);
  149. free(val);
  150. return ptrlen;
  151. }
  152. #ifdef CURL_HAS_SOCKOPT
  153. int json_rpc_call_sockopt_cb(void __maybe_unused *userdata, curl_socket_t fd,
  154. curlsocktype __maybe_unused purpose)
  155. {
  156. int tcp_keepidle = 120;
  157. int tcp_keepintvl = 120;
  158. #ifndef WIN32
  159. int keepalive = 1;
  160. int tcp_keepcnt = 5;
  161. if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive))))
  162. return 1;
  163. # ifdef __linux
  164. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &tcp_keepcnt, sizeof(tcp_keepcnt))))
  165. return 1;
  166. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle))))
  167. return 1;
  168. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  169. return 1;
  170. # endif /* __linux */
  171. # ifdef __APPLE_CC__
  172. if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  173. return 1;
  174. # endif /* __APPLE_CC__ */
  175. #else /* WIN32 */
  176. struct tcp_keepalive vals;
  177. vals.onoff = 1;
  178. vals.keepalivetime = tcp_keepidle * 1000;
  179. vals.keepaliveinterval = tcp_keepintvl * 1000;
  180. DWORD outputBytes;
  181. if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals), NULL, 0, &outputBytes, NULL, NULL)))
  182. return 1;
  183. #endif /* WIN32 */
  184. return 0;
  185. }
  186. #endif
  187. static void last_nettime(struct timeval *last)
  188. {
  189. rd_lock(&netacc_lock);
  190. last->tv_sec = nettime.tv_sec;
  191. last->tv_usec = nettime.tv_usec;
  192. rd_unlock(&netacc_lock);
  193. }
  194. static void set_nettime(void)
  195. {
  196. wr_lock(&netacc_lock);
  197. gettimeofday(&nettime, NULL);
  198. wr_unlock(&netacc_lock);
  199. }
  200. json_t *json_rpc_call(CURL *curl, const char *url,
  201. const char *userpass, const char *rpc_req,
  202. bool probe, bool longpoll, bool *rolltime,
  203. struct pool *pool, bool share)
  204. {
  205. json_t *val, *err_val, *res_val;
  206. int rc;
  207. struct data_buffer all_data = {NULL, 0};
  208. struct upload_buffer upload_data;
  209. json_error_t err;
  210. struct curl_slist *headers = NULL;
  211. char len_hdr[64], user_agent_hdr[128];
  212. char curl_err_str[CURL_ERROR_SIZE];
  213. long timeout = longpoll ? (60 * 60) : 60;
  214. struct header_info hi = {NULL, false, NULL};
  215. bool probing = false;
  216. memset(&err, 0, sizeof(err));
  217. /* it is assumed that 'curl' is freshly [re]initialized at this pt */
  218. if (probe)
  219. probing = !pool->probed;
  220. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  221. #if 0 /* Disable curl debugging since it spews to stderr */
  222. if (opt_protocol)
  223. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  224. #endif
  225. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  226. curl_easy_setopt(curl, CURLOPT_URL, url);
  227. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  228. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  229. /* Shares are staggered already and delays in submission can be costly
  230. * so do not delay them */
  231. if (!opt_delaynet || share)
  232. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  233. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  234. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  235. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  236. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  237. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  238. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  239. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
  240. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
  241. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
  242. if (opt_socks_proxy) {
  243. curl_easy_setopt(curl, CURLOPT_PROXY, opt_socks_proxy);
  244. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  245. }
  246. if (userpass) {
  247. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  248. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  249. }
  250. #ifdef CURL_HAS_SOCKOPT
  251. if (longpoll)
  252. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, json_rpc_call_sockopt_cb);
  253. #endif
  254. curl_easy_setopt(curl, CURLOPT_POST, 1);
  255. if (opt_protocol)
  256. applog(LOG_DEBUG, "JSON protocol request:\n%s", rpc_req);
  257. upload_data.buf = rpc_req;
  258. upload_data.len = strlen(rpc_req);
  259. sprintf(len_hdr, "Content-Length: %lu",
  260. (unsigned long) upload_data.len);
  261. sprintf(user_agent_hdr, "User-Agent: %s", PACKAGE_STRING);
  262. headers = curl_slist_append(headers,
  263. "Content-type: application/json");
  264. headers = curl_slist_append(headers,
  265. "X-Mining-Extensions: longpoll midstate rollntime submitold");
  266. headers = curl_slist_append(headers, len_hdr);
  267. headers = curl_slist_append(headers, user_agent_hdr);
  268. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  269. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  270. if (opt_delaynet) {
  271. /* Don't delay share submission, but still track the nettime */
  272. if (!share) {
  273. long long now_msecs, last_msecs;
  274. struct timeval now, last;
  275. gettimeofday(&now, NULL);
  276. last_nettime(&last);
  277. now_msecs = (long long)now.tv_sec * 1000;
  278. now_msecs += now.tv_usec / 1000;
  279. last_msecs = (long long)last.tv_sec * 1000;
  280. last_msecs += last.tv_usec / 1000;
  281. if (now_msecs > last_msecs && now_msecs - last_msecs < 250) {
  282. struct timespec rgtp;
  283. rgtp.tv_sec = 0;
  284. rgtp.tv_nsec = (250 - (now_msecs - last_msecs)) * 1000000;
  285. nanosleep(&rgtp, NULL);
  286. }
  287. }
  288. set_nettime();
  289. }
  290. rc = curl_easy_perform(curl);
  291. if (longpoll)
  292. pool_tclear(pool, &pool->lp_sent);
  293. if (rc) {
  294. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  295. goto err_out;
  296. }
  297. if (!all_data.buf) {
  298. applog(LOG_DEBUG, "Empty data received in json_rpc_call.");
  299. goto err_out;
  300. }
  301. if (probing) {
  302. pool->probed = true;
  303. /* If X-Long-Polling was found, activate long polling */
  304. if (hi.lp_path) {
  305. if (pool->hdr_path != NULL)
  306. free(pool->hdr_path);
  307. pool->hdr_path = hi.lp_path;
  308. } else {
  309. pool->hdr_path = NULL;
  310. }
  311. } else if (hi.lp_path) {
  312. free(hi.lp_path);
  313. }
  314. *rolltime = hi.has_rolltime;
  315. val = JSON_LOADS(all_data.buf, &err);
  316. if (!val) {
  317. applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text);
  318. if (opt_protocol)
  319. applog(LOG_DEBUG, "JSON protocol response:\n%s", all_data.buf);
  320. goto err_out;
  321. }
  322. if (opt_protocol) {
  323. char *s = json_dumps(val, JSON_INDENT(3));
  324. applog(LOG_DEBUG, "JSON protocol response:\n%s", s);
  325. free(s);
  326. }
  327. /* JSON-RPC valid response returns a non-null 'result',
  328. * and a null 'error'.
  329. */
  330. res_val = json_object_get(val, "result");
  331. err_val = json_object_get(val, "error");
  332. if (!res_val || json_is_null(res_val) ||
  333. (err_val && !json_is_null(err_val))) {
  334. char *s;
  335. if (err_val)
  336. s = json_dumps(err_val, JSON_INDENT(3));
  337. else
  338. s = strdup("(unknown reason)");
  339. applog(LOG_INFO, "JSON-RPC call failed: %s", s);
  340. free(s);
  341. goto err_out;
  342. }
  343. if (hi.reason) {
  344. json_object_set_new(val, "reject-reason", json_string(hi.reason));
  345. free(hi.reason);
  346. hi.reason = NULL;
  347. }
  348. successful_connect = true;
  349. databuf_free(&all_data);
  350. curl_slist_free_all(headers);
  351. curl_easy_reset(curl);
  352. return val;
  353. err_out:
  354. databuf_free(&all_data);
  355. curl_slist_free_all(headers);
  356. curl_easy_reset(curl);
  357. if (!successful_connect)
  358. applog(LOG_DEBUG, "Failed to connect in json_rpc_call");
  359. curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
  360. return NULL;
  361. }
  362. char *bin2hex(const unsigned char *p, size_t len)
  363. {
  364. unsigned int i;
  365. char *s = malloc((len * 2) + 1);
  366. if (!s)
  367. return NULL;
  368. for (i = 0; i < len; i++)
  369. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  370. return s;
  371. }
  372. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  373. {
  374. while (*hexstr && len) {
  375. char hex_byte[3];
  376. unsigned int v;
  377. if (!hexstr[1]) {
  378. applog(LOG_ERR, "hex2bin str truncated");
  379. return false;
  380. }
  381. hex_byte[0] = hexstr[0];
  382. hex_byte[1] = hexstr[1];
  383. hex_byte[2] = 0;
  384. if (sscanf(hex_byte, "%x", &v) != 1) {
  385. applog(LOG_ERR, "hex2bin sscanf '%s' failed", hex_byte);
  386. return false;
  387. }
  388. *p = (unsigned char) v;
  389. p++;
  390. hexstr += 2;
  391. len--;
  392. }
  393. return (len == 0 && *hexstr == 0) ? true : false;
  394. }
  395. /* Subtract the `struct timeval' values X and Y,
  396. storing the result in RESULT.
  397. Return 1 if the difference is negative, otherwise 0. */
  398. int
  399. timeval_subtract (
  400. struct timeval *result, struct timeval *x, struct timeval *y)
  401. {
  402. /* Perform the carry for the later subtraction by updating Y. */
  403. if (x->tv_usec < y->tv_usec) {
  404. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  405. y->tv_usec -= 1000000 * nsec;
  406. y->tv_sec += nsec;
  407. }
  408. if (x->tv_usec - y->tv_usec > 1000000) {
  409. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  410. y->tv_usec += 1000000 * nsec;
  411. y->tv_sec -= nsec;
  412. }
  413. /* Compute the time remaining to wait.
  414. `tv_usec' is certainly positive. */
  415. result->tv_sec = x->tv_sec - y->tv_sec;
  416. result->tv_usec = x->tv_usec - y->tv_usec;
  417. /* Return 1 if result is negative. */
  418. return x->tv_sec < y->tv_sec;
  419. }
  420. bool fulltest(const unsigned char *hash, const unsigned char *target)
  421. {
  422. unsigned char hash_swap[32], target_swap[32];
  423. uint32_t *hash32 = (uint32_t *) hash_swap;
  424. uint32_t *target32 = (uint32_t *) target_swap;
  425. int i;
  426. bool rc = true;
  427. char *hash_str, *target_str;
  428. swap256(hash_swap, hash);
  429. swap256(target_swap, target);
  430. for (i = 0; i < 32/4; i++) {
  431. uint32_t h32tmp = swab32(hash32[i]);
  432. uint32_t t32tmp = target32[i];
  433. target32[i] = swab32(target32[i]); /* for printing */
  434. if (h32tmp > t32tmp) {
  435. rc = false;
  436. break;
  437. }
  438. if (h32tmp < t32tmp) {
  439. rc = true;
  440. break;
  441. }
  442. }
  443. if (opt_debug) {
  444. hash_str = bin2hex(hash_swap, 32);
  445. target_str = bin2hex(target_swap, 32);
  446. applog(LOG_DEBUG, " Proof: %s\nTarget: %s\nTrgVal? %s",
  447. hash_str,
  448. target_str,
  449. rc ? "YES (hash < target)" :
  450. "no (false positive; hash > target)");
  451. free(hash_str);
  452. free(target_str);
  453. }
  454. return rc;
  455. }
  456. struct thread_q *tq_new(void)
  457. {
  458. struct thread_q *tq;
  459. tq = calloc(1, sizeof(*tq));
  460. if (!tq)
  461. return NULL;
  462. INIT_LIST_HEAD(&tq->q);
  463. pthread_mutex_init(&tq->mutex, NULL);
  464. pthread_cond_init(&tq->cond, NULL);
  465. return tq;
  466. }
  467. void tq_free(struct thread_q *tq)
  468. {
  469. struct tq_ent *ent, *iter;
  470. if (!tq)
  471. return;
  472. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  473. list_del(&ent->q_node);
  474. free(ent);
  475. }
  476. pthread_cond_destroy(&tq->cond);
  477. pthread_mutex_destroy(&tq->mutex);
  478. memset(tq, 0, sizeof(*tq)); /* poison */
  479. free(tq);
  480. }
  481. static void tq_freezethaw(struct thread_q *tq, bool frozen)
  482. {
  483. mutex_lock(&tq->mutex);
  484. tq->frozen = frozen;
  485. pthread_cond_signal(&tq->cond);
  486. mutex_unlock(&tq->mutex);
  487. }
  488. void tq_freeze(struct thread_q *tq)
  489. {
  490. tq_freezethaw(tq, true);
  491. }
  492. void tq_thaw(struct thread_q *tq)
  493. {
  494. tq_freezethaw(tq, false);
  495. }
  496. bool tq_push(struct thread_q *tq, void *data)
  497. {
  498. struct tq_ent *ent;
  499. bool rc = true;
  500. ent = calloc(1, sizeof(*ent));
  501. if (!ent)
  502. return false;
  503. ent->data = data;
  504. INIT_LIST_HEAD(&ent->q_node);
  505. mutex_lock(&tq->mutex);
  506. if (!tq->frozen) {
  507. list_add_tail(&ent->q_node, &tq->q);
  508. } else {
  509. free(ent);
  510. rc = false;
  511. }
  512. pthread_cond_signal(&tq->cond);
  513. mutex_unlock(&tq->mutex);
  514. return rc;
  515. }
  516. void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
  517. {
  518. struct tq_ent *ent;
  519. void *rval = NULL;
  520. int rc;
  521. mutex_lock(&tq->mutex);
  522. if (!list_empty(&tq->q))
  523. goto pop;
  524. if (abstime)
  525. rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
  526. else
  527. rc = pthread_cond_wait(&tq->cond, &tq->mutex);
  528. if (rc)
  529. goto out;
  530. if (list_empty(&tq->q))
  531. goto out;
  532. pop:
  533. ent = list_entry(tq->q.next, struct tq_ent, q_node);
  534. rval = ent->data;
  535. list_del(&ent->q_node);
  536. free(ent);
  537. out:
  538. mutex_unlock(&tq->mutex);
  539. return rval;
  540. }
  541. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg)
  542. {
  543. int ret;
  544. ret = pthread_create(&thr->pth, attr, start, arg);
  545. return ret;
  546. }
  547. void thr_info_freeze(struct thr_info *thr)
  548. {
  549. struct tq_ent *ent, *iter;
  550. struct thread_q *tq;
  551. if (!thr)
  552. return;
  553. tq = thr->q;
  554. if (!tq)
  555. return;
  556. mutex_lock(&tq->mutex);
  557. tq->frozen = true;
  558. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  559. list_del(&ent->q_node);
  560. free(ent);
  561. }
  562. mutex_unlock(&tq->mutex);
  563. }
  564. void thr_info_cancel(struct thr_info *thr)
  565. {
  566. if (!thr)
  567. return;
  568. if (PTH(thr) != 0L) {
  569. pthread_cancel(thr->pth);
  570. PTH(thr) = 0L;
  571. }
  572. }