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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
|
/*
* myth.h --- toplevel include file of MassiveThreads
*/
#pragma once
#ifndef MYTH_H_
#define MYTH_H_
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include "myth/myth_spinlock.h"
#include "myth/myth_sleep_queue.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
at this point we define myth_thread_t
to be an opaque pointer. struct myth_thread
is defined in myth_desc.h
*/
typedef struct myth_thread * myth_thread_t;
/* ---------------------------------------
--- mutex ---
--------------------------------------- */
enum {
MYTH_MUTEX_NORMAL = 0,
MYTH_MUTEX_ERRORCHECK = 1,
MYTH_MUTEX_RECURSIVE = 2,
MYTH_MUTEX_INVALID = 3,
MYTH_MUTEX_DEFAULT = MYTH_MUTEX_NORMAL
};
typedef struct myth_mutexattr {
int type; /* one of the above constants */
} myth_mutexattr_t;
typedef struct myth_mutex {
int magic;
myth_mutexattr_t attr;
myth_sleep_queue_t sleep_q[1];
volatile long state; /* n_waiters|locked */
} myth_mutex_t;
enum { myth_mutex_magic_no = 123456789,
myth_mutex_magic_no_initializing = 987654321 };
#define MYTH_MUTEX_INITIALIZER { myth_mutex_magic_no, { MYTH_MUTEX_DEFAULT }, { MYTH_SLEEP_QUEUE_INITIALIZER }, 0 }
/* ---------------------------------------
--- reader-writer lock ---
--------------------------------------- */
enum {
MYTH_RWLOCK_PREFER_READER,
MYTH_RWLOCK_PREFER_WRITER,
MYTH_RWLOCK_PREFER_WRITER_NONRECURSIVE,
MYTH_RWLOCK_PREFER_INVALID
};
typedef struct myth_rwlockattr {
int kind;
} myth_rwlockattr_t;
typedef struct myth_rwlock {
myth_sleep_queue_t sleep_q[1];
volatile long state;
myth_rwlockattr_t attr;
} myth_rwlock_t;
#define MYTH_RWLOCK_INITIALIZER { { MYTH_SLEEP_QUEUE_INITIALIZER }, 0, { MYTH_RWLOCK_DEFAULT } }
/* ---------------------------------------
--- condition variable ---
--------------------------------------- */
typedef struct myth_condattr {
void * unused; /* just to suppress warning against empty struct */
} myth_condattr_t;
//Conditional variable data structure
typedef struct myth_cond {
myth_sleep_queue_t sleep_q[1];
myth_condattr_t attr;
} myth_cond_t;
#define MYTH_COND_INITIALIZER { { MYTH_SLEEP_QUEUE_INITIALIZER }, { } }
/* ---------------------------------------
--- barrier ---
--------------------------------------- */
typedef struct myth_barrierattr {
void * unused; /* just to suppress warning against empty struct */
} myth_barrierattr_t;
typedef struct myth_barrier {
long n_threads;
volatile long state;
//myth_sleep_queue_t sleep_q[1];
myth_sleep_stack_t sleep_s[1];
myth_barrierattr_t attr;
} myth_barrier_t;
#define MYTH_BARRIER_SERIAL_THREAD 1
/* ---------------------------------------
--- join counter ---
--------------------------------------- */
//Join counter data structure
typedef struct myth_join_counterattr {
void * unused; /* just to suppress warning against empty struct */
} myth_join_counterattr_t;
typedef struct myth_join_counter {
/* TODO: conserve space? */
long n_threads; /* const : number of decrements to see */
int n_threads_bits; /* number of bits to represent n_threads */
long state_mask; /* (1 << n_threads_bits) - 1 */
volatile long state;
myth_sleep_queue_t sleep_q[1];
myth_join_counterattr_t attr;
} myth_join_counter_t;
/* ---------------------------------------
--- full empty lock ---
--------------------------------------- */
//Full/empty lock data structure
typedef struct myth_felockattr {
void * unused; /* just to suppress warning against empty struct */
} myth_felockattr_t;
typedef struct myth_felock {
myth_mutex_t mutex[1];
myth_cond_t cond[2];
int status;
myth_felockattr_t attr;
} myth_felock_t;
/* ---------------------------------------
--- uncondition variable ---
--------------------------------------- */
typedef struct {
volatile myth_thread_t th; /* the thread sleeping on it */
} myth_uncond_t;
/* ---------------------------------------
--- global attributes and initialization functions ---
--------------------------------------- */
typedef struct {
size_t stacksize;
size_t guardsize;
int n_workers;
int bind_workers;
int child_first;
int initialized;
} myth_globalattr_t;
/*
Function: myth_init
Initialize MassiveThreads library with
default parameters (see <myth_init_ex>).
You normally do not have to call it by
youself, as it is automatically called
when you first call any MassiveThreads
function. Internally, it invokes underlying
workers. You may want to call it to make
absolutely sure that an initilization has
taken place before a certain point.
Returns:
zero if the library has been successfully initialized.
non-zero otherwise.
See Also:
<myth_init_ex>
*/
int myth_init(void);
/*
Function: myth_init_ex
Initialize MassiveThreads library with the
specified global attributes. You
normally do not have to call it by
youself, as <myth_init> is automatically
called when you first call any
MassiveThreads function. You may want to
call it to explicitly set various attributes.
Parameters:
attr - the pointer to global attribute
Returns:
zero if the library has been successfully initialized.
non-zero otherwise.
See Also:
<myth_init>, <myth_create>, <myth_create_ex>
*/
int myth_init_ex(myth_globalattr_t * attr);
/*
Function: myth_fini
Finalize MassiveThreads.
See Also: <myth_init>, <myth_init_ex>
*/
void myth_fini(void);
/*
Function: myth_globalattr_init
Parameters:
attr - global attribute to initialize
initialize global attributes of MassiveThreads to default values.
you can then set various attributes using one of
myth_globalattr_set_ATTRIBUTE functions.
See Also: <myth_init_ex>,
<myth_globalattr_destroy>,
<myth_globalattr_get_stacksize>, <myth_globalattr_set_stacksize>,
<myth_globalattr_get_n_workers>, <myth_globalattr_set_n_workers>,
<myth_globalattr_get_bind_workers>, <myth_globalattr_set_bind_workers>
*/
int myth_globalattr_init(myth_globalattr_t * attr);
/*
Function: myth_globalattr_destroy
Parameters:
attr - global attribute to destroy
destroy global attributes of MassiveThreads.
See Also: <myth_init>, <myth_init_ex>, <myth_globalattr_init>
*/
int myth_globalattr_destroy(myth_globalattr_t * attr);
/*
Function: myth_globalattr_get_stacksize
Parameters:
attr - global attribute to get stack size of
stacksize - address to get the result in
get the stack size attribute in attr, set either by
myth_globalattr_init or myth_globalattr_set_stacksize
See Also:
<myth_globalattr_init>, <myth_globalattr_set_stacksize>
*/
int myth_globalattr_get_stacksize(myth_globalattr_t * attr,
size_t *stacksize);
/*
Function: myth_globalattr_set_stacksize
Parameters:
attr - global attribute to set stack size of
stacksize - the stack size to set
set the stack size attribute in attr to the specified stacksize.
See Also:
<myth_globalattr_init>, <myth_globalattr_get_stacksize>
*/
int myth_globalattr_set_stacksize(myth_globalattr_t * attr,
size_t stacksize);
/*
Function: myth_globalattr_get_n_workers
Parameters:
attr - global attribute to get the number of workers of
n_workers - the address to get the number of workers in
get the number of workers attribute in attr, set either
by myth_globalattr_init or myth_globalattr_set_n_workers
See Also:
<myth_globalattr_init>, <myth_globalattr_set_n_workers>
*/
int myth_globalattr_get_n_workers(myth_globalattr_t * attr,
size_t *n_workers);
/*
Function: myth_globalattr_set_n_workers
Parameters:
attr - global attribute to set the number of workers of
n_workers - the number of workers
set the number of workers attribute of attr
See Also:
<myth_globalattr_init>, <myth_globalattr_get_n_workers>
*/
int myth_globalattr_set_n_workers(myth_globalattr_t * attr,
size_t n_workers);
/*
Function: myth_globalattr_get_bind_workers
Parameters:
attr - global attribute to get the bind_workers attribute of
bind_workers - the address to get the bind_workers attribute in
get the bind_workers attribute in attr, set either
by myth_globalattr_init or myth_globalattr_set_bind_workers
See Also:
<myth_globalattr_init>, <myth_globalattr_set_bind_workers>
*/
int myth_globalattr_get_bind_workers(myth_globalattr_t * attr,
int *bind_workers);
/*
Function: myth_globalattr_set_bind_workers
Parameters:
attr - global attribute to set the bind_workers attribute of
bind_workers - 1 or 0. 1 specified each worker should be bound to
a core.
set the bind_workers attribute of attr
See Also:
<myth_globalattr_init>, <myth_globalattr_get_bind_workers>
*/
int myth_globalattr_set_bind_workers(myth_globalattr_t * attr,
int bind_workers);
typedef struct myth_thread_attr {
void * stackaddr;
size_t stacksize;
size_t guardsize;
int detachstate;
int child_first;
/* TODO: get rid of them */
size_t custom_data_size;
void *custom_data;
} myth_thread_attr_t;
typedef void*(*myth_func_t)(void*);
/*
Function: myth_create
Create a new user-level thread executing func(arg)
with default options. Note that it is equivalent
to myth_create_ex(func,arg,0);
Parameters:
func - a pointer to a function.
arg - a pointer given to func.
Returns:
The identifier of the newly created user-level thread.
Bug:
Should any error occur, it terminates the
program rather than returning an error
code.
See Also:
<myth_create_ex>, <myth_join>
*/
myth_thread_t myth_create(myth_func_t func, void *arg);
/*
Function: myth_create_ex
Create a new user-level thread executing func(arg)
with specified options.
Parameters:
id - a pointer, if not NULL, to which id of the created thread will be stored.
func - a pointer to a function.
arg - a pointer given to func.
attr - a pointer to a data structure
of type <myth_thread_attr_t>
specifying thread attributes, or NULL
to mean the deafult.
Returns:
0 if succeed.
Bug:
Should any error occur, it terminates the
program rather than returning an error
code.
See Also:
<myth_create>, <myth_join>, <myth_thread_option>
*/
int myth_create_ex(myth_thread_t * id, myth_thread_attr_t * attr,
myth_func_t func, void * arg);
myth_thread_t myth_create_nosched(myth_func_t func, void * arg,
myth_thread_attr_t * attr);
/*
Function: myth_exit
Terminate the calling user-level thread.
Parameters:
ret - exit value of the thread, which can
be retrieved by calling <myth_join> on
this thread.
See Also:
<myth_join>
*/
void myth_exit(void *ret);
/*
Function: myth_join
Wait for the specified thread th to finish.
Parameters:
th - the identifier of the thread to wait for
result - a pointer to a data structure receiving
the exit value of the thread, as determined by
<myth_exit> or the return value of the thread's
main function.
See Also:
<myth_create>, <myth_create_ex>
*/
int myth_join(myth_thread_t th, void **result);
int myth_tryjoin(myth_thread_t th,void **result);
int myth_timedjoin(myth_thread_t th, void **result,
const struct timespec *abstime);
/*
Function: myth_create_join_many_ex
Create many user-level threads executing the same function
with various arguments and attributes and wait for them
to complete.
Parameters:
ids - base pointer to a (strided) array, to which thread ids of
the created threads wll be stored (may be NULL)
attrs - base pointer to a (strided) array specifying attributes of
threads to create (may be NULL)
func - a function to execute by each thread
args - base pointer to a (strided) array specifying arguments to func
results - base pointer to a (strided) array to which results of the
function call will be stored (may be NULL)
id_stride - the stride of the ids array, in bytes
attr_stride - the stride of the attrs array, in bytes
arg_stride - the stride of the args array, in bytes
result_stride - the stride of the results array, in bytes
long nthreads - number of threads to execute f
in its simplest form,
myth_create_join_many_ex(0, 0, f, X, 0,
0, 0, s, 0, n);
will execute f(args), f(args+s), f(args+2*s), ..., and f(args+(n-1)*s),
each by a separate thread and discard their return values.
if you want to get return values, give results and result_stride. e.g.,
myth_create_join_many_ex(0, 0, f, X, Y,
0, 0, xs, t, n);
is equivalent to:
for all i = 0, ..., n - 1
((void **)(Y + i * t))[0] = f(args + i * s);
note that all stride arguments must be given in bytes.
this is to allow you to receive results in a field of
an enclosing structure. e.g.,
struct { char stuff[100]; void * result } args[nthreads];
in this case you want to call this function with
results = &args[0].result and result_stride = sizeof(args[0]);
consistent with this policy, results is a void pointer,
although it is internally used as (void **).
You can similarly specify addresses of attributes and thread ids,
using the base pointer and the stride.
Returns:
0 if succeed.
Bug:
Should any error occur, it terminates the
program rather than returning an error
code.
See Also:
<myth_create>, <myth_join>, <myth_thread_attr>
*/
int myth_create_join_many_ex(myth_thread_t * ids,
myth_thread_attr_t * attrs,
myth_func_t func,
void * args,
void * results,
size_t id_stride,
size_t attr_stride,
size_t arg_stride,
size_t result_stride,
long nthreads);
/*
Function: myth_create_join_various_ex
Create many user-level threads executing
various functions with various arguments
and attributes and wait for them to
complete. This is almost the same with
myth_create_join_many_ex, except that you
can have threads execute different
functions.
Parameters:
ids - base pointer to a (strided) array, to which thread ids of
the created threads wll be stored (may be NULL)
attrs - base pointer to a (strided) array specifying attributes of
threads to create (may be NULL)
funcs - base pointer to a (strided) array specifying functions
to execute
args - base pointer to a (strided) array specifying arguments to func
results - base pointer to a (strided) array to which results of the
function call will be stored (may be NULL)
id_stride - the stride of the ids array, in bytes
attr_stride - the stride of the attrs array, in bytes
func_stride - the stride of the funcs array, in bytes
arg_stride - the stride of the args array, in bytes
result_stride - the stride of the results array, in bytes
long nthreads - number of threads to execute f
in its simplest form,
myth_create_join_many_ex(0, 0, F, X, 0,
0, 0, fs, xs, 0, n);
will execute f_0(args), f_1(args+xs), f_2(args+2*xs), ...,
where f_i = *((myth_func_t *)(F + fs * i)),
each by a separate thread and discard their return values.
if you want to get return values, give results and result_stride. e.g.,
myth_create_join_many_ex(0, 0, f, X, Y,
0, 0, s, t, n);
is equivalent to:
for all i = 0, ..., n - 1
((void **)(Y + i * t))[0] = f(args + i * s);
note that all stride arguments must be given in bytes.
this is to allow you to receive results in a field of
an enclosing structure. e.g.,
struct { char stuff[100]; void * result } args[nthreads];
in this case you want to call this function with
results = &args[0].result and result_stride = sizeof(args[0]);
consistent with this policy, results is a void pointer,
although it is internally used as (void **).
You can similarly specify addresses of attributes and thread ids,
using the base pointer and the stride.
Returns:
0 if succeed.
Bug:
Should any error occur, it terminates the
program rather than returning an error
code.
See Also:
<myth_create>, <myth_join>, <myth_thread_attr>
*/
int myth_create_join_various_ex(myth_thread_t * ids,
myth_thread_attr_t * attrs,
myth_func_t * funcs,
void * args,
void * results,
size_t id_stride,
size_t attr_stride,
size_t func_stride,
size_t arg_stride,
size_t result_stride,
long nthreads);
/*
Function: myth_detach
*/
int myth_detach(myth_thread_t th);
/*
Function: myth_is_myth_worker
Returns:
1 if the calling OS-level thread is a massivethreads
worker. useful when you mix pthreads and massivethreads.
*/
int myth_is_myth_worker(void);
/*
Function: myth_self
Returns:
The identifier of the calling thread.
See Also:
<myth_get_worker_num>, <myth_get_num_workers>
*/
myth_thread_t myth_self(void);
/*
Function: myth_equal
*/
int myth_equal(myth_thread_t t1, myth_thread_t t2);
/*
Function: myth_thread_attr_init
*/
int myth_thread_attr_init(myth_thread_attr_t * attr);
/*
Function: myth_thread_attr_getdetachstate
*/
int myth_thread_attr_getdetachstate(const myth_thread_attr_t *attr,
int *detachstate);
/*
Function: myth_thread_attr_setdetachstate
*/
int myth_thread_attr_setdetachstate(myth_thread_attr_t *attr,
int detachstate);
/*
Function: myth_thread_attr_getguardsize
*/
int myth_thread_attr_getguardsize(const myth_thread_attr_t *attr,
size_t *guardsize);
/*
Function: myth_thread_attr_setguardsize
*/
int myth_thread_attr_setguardsize(myth_thread_attr_t *attr, size_t guardsize);
/*
Function: myth_thread_attr_getstacksize
*/
int myth_thread_attr_getstacksize(const myth_thread_attr_t *attr,
size_t *stacksize);
/*
Function: myth_thread_attr_setstacksize
*/
int myth_thread_attr_setstacksize(myth_thread_attr_t *attr, size_t stacksize);
/*
Function: myth_thread_attr_getstack
*/
int myth_thread_attr_getstack(const myth_thread_attr_t *attr,
void **stackaddr, size_t *stacksize);
/*
Function: myth_thread_attr_setstack
*/
int myth_thread_attr_setstack(myth_thread_attr_t *attr,
void *stackaddr, size_t stacksize);
/*
Function: myth_getattr_default_np
*/
int myth_getattr_default_np(myth_thread_attr_t *attr);
/*
Function: myth_getattr_np
*/
int myth_getattr_np(myth_thread_t thread, myth_thread_attr_t *attr);
/*
Function: myth_getconcurrency
*/
int myth_getconcurrency(void);
enum {
myth_yield_option_half_half = 0,
myth_yield_option_local_only = 1,
myth_yield_option_local_first = 2,
myth_yield_option_steal_only = 3,
myth_yield_option_steal_first = 4
};
/*
Function: myth_yield_ex
Yield execution to another user-level thread.
Parameters:
yield_opt - take one of the following values and change
the behavior.
myth_yield_option_half_half :
behave like myth_yield_option_local_first with probability 1/2
and like myth_yield_option_steal_first with probability 1/2
myth_yield_option_local_only :
try to yield to another thread in the local run queue.
if none exist, the caller keeps running.
myth_yield_option_local_first :
try to yield to another thread in the local run queue.
if none exist, an attempt is made to steal another thread
in a remote run queue; if it succeeds, yields to it. otherwise
keep running.
myth_yield_option_steal_only :
an attempt is made to steal another thread
in a remote run queue; if it succeeds, yield to it. otherwise
keep running.
myth_yield_option_steal_first :
an attempt is made to steal another thread
in a remote run queue; if it succeeds, yield to it. otherwise
try to yield to another thread in the local run queue.
if none exist, the caller keeps running.
Note:
Available options as well as detailed behaviors may change in future.
See Also:
<myth_yield>
*/
void myth_yield_ex(int yield_opt);
/*
Function: myth_yield
it is equivalent to myth_yield_ex(myth_yield_option_half_half);
with probability 1/2, try to yield to a thread in the local
queue and if none is found try to steal a thread from a remote
queue. do the opposite with probability 1/2.
See Also:
<myth_yield>
Note:
The above describes the current implementation,
which may change in future. You should not rely
on its exact behavior (other than it switches
to another user-level thread).
*/
void myth_yield(void);
/*
Function: myth_setcancelstate
*/
int myth_setcancelstate(int state, int *oldstate);
/*
Function: myth_setcanceltype
*/
int myth_setcanceltype(int type, int *oldtype);
/*
Function: myth_cancel
*/
int myth_cancel(myth_thread_t th);
/*
Function: myth_testcancel
*/
void myth_testcancel(void);
enum {
myth_once_state_init = 0,
myth_once_state_in_progress = 1,
myth_once_state_completed = 2
};
/*
Type: myth_once_t
*/
typedef struct {
volatile int state;
} myth_once_t;
/*
Function: myth_once
*/
int myth_once(myth_once_t * once_control, void (*init_routine)(void));
/*
Function: myth_mutex_init
Initialize a mutex.
Parameters:
mutex - a pointer to a mutex data structure to initialize.
attr - a pointer to mutex attributes.
Returns:
zero if suceeds or an errno.
See Also:
<myth_mutex_destroy>, <myth_mutex_lock>, <myth_mutex_trylock>, <myth_mutex_unlock>
*/
int myth_mutex_init(myth_mutex_t * mutex,
const myth_mutexattr_t * attr);
/*
Function: myth_mutex_destroy
Destroy a mutex.
Parameters:
mutex - a pointer to a mutex data structure to initialize.
Returns:
zero if suceeds or an errno.
See Also:
<myth_mutex_init>, <myth_mutex_lock>, <myth_mutex_trylock>, <myth_mutex_unlock>
*/
int myth_mutex_destroy(myth_mutex_t * mutex);
/*
Function: myth_mutex_trylock
Try to lock a mutex.
Parameters:
mutex - a mutex to try to lock.
Returns:
zero if it successfully acquired a lock.
an errno otherwise.
See Also:
<myth_mutex_init>, <myth_mutex_destroy>, <myth_mutex_lock>, <myth_mutex_unlock>
*/
int myth_mutex_trylock(myth_mutex_t * mtx);
/*
Function: myth_mutex_lock
Lock a mutex.
Parameters:
mutex - a mutex to lock.
Returns:
zero if suceeds or an errno when an error occurred.
See Also:
<myth_mutex_init>, <myth_mutex_destroy>, <myth_mutex_trylock>, <myth_mutex_unlock>
*/
int myth_mutex_lock(myth_mutex_t * mtx);
/*
Function: myth_mutex_timedlock
Lock a mutex.
Parameters:
mutex - a mutex to lock.
abstime - absolute time the function returns when the lock cannot be acquired
Returns:
zero if suceeds or an errno when an error occurred.
See Also:
<myth_mutex_init>, <myth_mutex_destroy>, <myth_mutex_trylock>, <myth_mutex_unlock>
*/
int myth_mutex_timedlock(myth_mutex_t * mtx, const struct timespec * abstime); // const struct timespec *restrict abstime
/*
Function: myth_mutex_unlock
Unlock a mutex.
Parameters:
mutex - a mutex to unlock.
Returns:
zero if suceeds or an errno when an error occurred.
See Also:
<myth_mutex_init>, <myth_mutex_destroy>, <myth_mutex_lock>, <myth_mutex_trylock>
*/
int myth_mutex_unlock(myth_mutex_t * mtx);
/*
Function: myth_mutexattr_init
*/
int myth_mutexattr_init(myth_mutexattr_t *attr);
/*
Function: myth_mutexattr_destroy
*/
int myth_mutexattr_destroy(myth_mutexattr_t *attr);
/*
Function: myth_mutexattr_gettype
*/
int myth_mutexattr_gettype(const myth_mutexattr_t * attr,
int * type);
/*
Function: myth_mutexattr_settype
*/
int myth_mutexattr_settype(myth_mutexattr_t *attr, int type);
/*
Function: myth_rwlock_init
*/
int myth_rwlock_init(myth_rwlock_t * rwlock,
const myth_rwlockattr_t * attr);
/*
Function: myth_rwlock_destroy
*/
int myth_rwlock_destroy(myth_rwlock_t *rwlock);
/*
Function: myth_rwlock_rdlock
*/
int myth_rwlock_rdlock(myth_rwlock_t *rwlock);
/*
Function: myth_rwlock_tryrdlock
*/
int myth_rwlock_tryrdlock(myth_rwlock_t *rwlock);
/*
Function: myth_rwlock_timedrdlock
*/
int myth_rwlock_timedrdlock(myth_rwlock_t * rwlock,
const struct timespec * abstime);
/*
Function: myth_rwlock_wrlock
*/
int myth_rwlock_wrlock(myth_rwlock_t *rwlock);
/*
Function: myth_rwlock_trywrlock
*/
int myth_rwlock_trywrlock(myth_rwlock_t *rwlock);
/*
Function: myth_rwlock_timedwrlock
*/
int myth_rwlock_timedwrlock(myth_rwlock_t * rwlock,
const struct timespec * abstime);
/*
Function: myth_rwlock_unlock
*/
int myth_rwlock_unlock(myth_rwlock_t *rwlock);
/*
Function: myth_rwlockattr_init
*/
int myth_rwlockattr_init(myth_rwlockattr_t *attr);
/*
Function: myth_rwlockattr_destroy
*/
int myth_rwlockattr_destroy(myth_rwlockattr_t *attr);
/*
Function: myth_rwlockattr_getkind
*/
int myth_rwlockattr_getkind(const myth_rwlockattr_t *attr, int *pref);
/*
Function: myth_rwlockattr_setkind
*/
int myth_rwlockattr_setkind(myth_rwlockattr_t *attr, int pref);
/*
Function: myth_cond_init
Initialize a condition variable.
Parameters:
cond - a pointer to a condition variable to initialize
attr - a pointer to condition variable attributes, or NULL
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_cond_destroy>, <myth_cond_wait>, <myth_cond_signal>, <myth_cond_broadcast>
*/
int myth_cond_init(myth_cond_t * cond,
const myth_condattr_t * attr);
/*
Function: myth_cond_destroy
Destroy a condition variable.
Parameters:
cond - a pointer to a condition variable to destroy.
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_cond_init>, <myth_cond_wait>, <myth_cond_signal>, <myth_cond_broadcast>
*/
int myth_cond_destroy(myth_cond_t * cond);
/*
Function: myth_cond_signal
Wake up at least one thread blocking on a condition variable.
Parameters:
cond - a pointer to a condition variable to signal.
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_cond_init>, <myth_cond_destroy>, <myth_cond_wait>, <myth_cond_broadcast>
*/
int myth_cond_signal(myth_cond_t * c);
/*
Function: myth_cond_broadcast
Wake up all threads blocking on a condition variable.
Parameters:
cond - a pointer to a condition variable from which threads
are to wake up.
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_cond_init>, <myth_cond_destroy>, <myth_cond_wait>, <myth_cond_signal>
*/
int myth_cond_broadcast(myth_cond_t * cond);
/*
Function: myth_cond_wait
Atomically unlock a mutex and block on a condition variable.
Parameters:
cond - a pointer to a condition variable to block on.
mutex - a pointer to a mutex to unlock
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_cond_init>, <myth_cond_destroy>, <myth_cond_signal>, <myth_cond_broadcast>
*/
int myth_cond_wait(myth_cond_t * cond, myth_mutex_t * mutex);
/*
Function: myth_cond_timedwait
*/
int myth_cond_timedwait(myth_cond_t * cond,
myth_mutex_t * mutex,
const struct timespec * abstime);
/*
Function: myth_condattr_init
*/
int myth_condattr_init(myth_condattr_t *attr);
/*
Function: myth_condattr_destroy
*/
int myth_condattr_destroy(myth_condattr_t *attr);
/*
Function: myth_barrier_init
Initialize a barrier.
Parameters:
barrier - a pointer to a barrier data structure to initialize.
attr - a pointer to barrier attributes
count - the number of threads going to synchronize with this barrier
Returns:
Zero if succeeded. An errno if failed.
*/
int myth_barrier_init(myth_barrier_t * barrier,
const myth_barrierattr_t * attr,
unsigned int count);
/*
Function: myth_barrier_destroy
Destroy a barrier.
Parameters:
barrier - a pointer to a barrier data structure to destroy.
*/
int myth_barrier_destroy(myth_barrier_t *barrier);
/*
Function: myth_barrier_wait
Wait on a barrier.
Parameters:
barrier - a pointer to a barrier data structure on which
the calling thread synchronizes
Returns:
When a barrier succeeds,
MYTH_BARRIER_SERIAL_THREAD is returned to
a single thread whereas zeros to other
threads. When a barrier fails,
an errno.
*/
int myth_barrier_wait(myth_barrier_t *barrier);
/*
Function: myth_barrierattr_init
*/
int myth_barrierattr_init(myth_barrierattr_t *attr);
/*
Function: myth_barrierattr_destroy
*/
int myth_barrierattr_destroy(myth_barrierattr_t *attr);
/*
Function: myth_join_counter_init
*/
int myth_join_counter_init(myth_join_counter_t * jc,
const myth_join_counterattr_t * attr, int val);
/*
Function: myth_join_counter_wait
*/
int myth_join_counter_wait(myth_join_counter_t * jc);
/*
Function: myth_join_counter_dec
*/
int myth_join_counter_dec(myth_join_counter_t * jc);
/*
Function: myth_join_counterattr_init
*/
int myth_join_counterattr_init(myth_join_counterattr_t * attr);
/*
Function: myth_join_counterattr_destroy
*/
int myth_join_counterattr_destroy(myth_join_counterattr_t * attr);
/* felock */
/*
Function: myth_felock_init
*/
int myth_felock_init(myth_felock_t * fe, const myth_felockattr_t * attr);
/*
Function: myth_felock_destroy
*/
int myth_felock_destroy(myth_felock_t * fe);
/*
Function: myth_felock_lock
*/
int myth_felock_lock(myth_felock_t * fe);
/*
Function: myth_felock_unlock
*/
int myth_felock_unlock(myth_felock_t * fe);
/*
Function: myth_felock_wait_and_lock
*/
int myth_felock_wait_and_lock(myth_felock_t * fe, int status_to_wait);
/*
Function: myth_felock_mark_and_signal
*/
int myth_felock_mark_and_signal(myth_felock_t * fe,int status_to_signal);
/*
Function: myth_felock_status
*/
int myth_felock_status(myth_felock_t * fe);
/*
Function: myth_felockattr_init
*/
int myth_felockattr_init(myth_felockattr_t * attr);
/*
Function: myth_felockattr_destroy
*/
int myth_felockattr_destroy(myth_felockattr_t * attr);
/*
Function: myth_uncond_init
initialize an uncondition variable.
Parameters:
uncond - a pointer to an unconditional variable data structure
to initialize
Returns:
zero if it succeeds and non-zero otherwise
*/
int myth_uncond_init(myth_uncond_t * uncond);
/*
Function: myth_uncond_destroy
destroy an uncondition variable.
Parameters:
uncond - a pointer to an unconditional variable data structure
to destroy
Returns:
zero if it succeeds and non-zero otherwise
*/
int myth_uncond_destroy(myth_uncond_t * u);
/*
Function: myth_uncond_wait
block on an uncondition variable, to be waken up later
by myth_uncond_signal. there can be only one thread
blocking on a single myth_uncond_t variable at the same time.
this function is typically called after
the caller checked a data structure and learned that
it cannot proceed (e.g., a caller thread trying to
get an element from a queue learned the queue is empty).
unlike cond_wait, however, it does not take an extra mutex variable
that is assumed to be held by the caller.
thus, it is the user's responsibility to implement a means
to resolve the race condition between the caller (P) and another
thread (Q) that might be changing the data structure concurrently.
unless correctly done, it might cause a deadlock bug;
if Q changes the data structure a moment
after P learned it cannot proceed but before
P enters myth_uncond_wait, Q might miss the opportunity
to wake up P. myth_uncond_signal waits until a thread blocks
on myth_uncond_t and wakes it up.
In summary, a typical (correct) sequence to use myth_uncond_wait
and myth_uncond_signal is as follows.
P:
1: atomically_change_data_to_indicate_I_am_sleeping;
2: myth_uncond_wait(u);
Q:
3: atomically_change_data_to_indicate_none_is_sleeping;
4: myth_uncond_signal(u);
line 1 and 3 must be done atomically with respect to each other.
when 1 succeds, a subsequent execution of line 3 by Q must witness
P should be blocking. when Q enters line 4, P might not have
executed line 2. myth_uncond_signal guarantees it waits for P
to enter.
Parameters:
uncond - a pointer to an unconditional variable data structure
on which the calling thread blocks.
Returns:
zero if it succeeds and non-zero otherwise
*/
int myth_uncond_wait(myth_uncond_t * uncond);
/*
Function: myth_uncond_signal
unblock the thread blocking on uncond.
even if no threads are found on uncond at the moment
of the call to this function, the caller _waits_ for
a thread to block on it, and then wakes it up.
in other words, this function _always_ wakes up a thread.
this function is typically called after
the caller checked a data structure and learned that
a thread should be blocked waiting for a condition to be met.
unlike cond_signal, this function does not assume there is a common
mutex protecting the data structure. therefore it is
the user's responsibility to implement a means for the
caller to be able to "learn that a thread should be blocked."
see the description of myth_uncond_wait for details.
Parameters:
uncond - a pointer to an unconditional variable data structure
on which a thread is blocked.
Returns:
zero if it succeeds and non-zero otherwise
*/
int myth_uncond_signal(myth_uncond_t * uncond);
typedef int myth_key_t;
/*
Function: myth_key_create
Create a key for user-level thread-specific data.
Parameters:
key - a pointer to which the created key will be stored.
destr_function - a pointer to a destructor function.
Returns:
Zero if succeed, or an errno when an error occurred.
Bug:
destr_function is ignored in the current implementation.
See Also:
<myth_key_delete>, <myth_setspecific>, <myth_getspecific>
*/
int myth_key_create(myth_key_t *key, void (*destr_function)(void *));
/*
Function: myth_key_delete
Delete a key for user-level thread-specific data.
Parameters:
key - key to delete
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_key_create>, <myth_setspecific>, <myth_getspecific>
*/
int myth_key_delete(myth_key_t key);
/*
Function: myth_setspecific
Associate a thread-specific data with a key.
Parameters:
key - a key created by myth_key_create
data - a data to be associated with key
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_key_create>, <myth_key_delete>, <myth_getspecific>
*/
int myth_setspecific(myth_key_t key, const void *data);
/*
Function: myth_getspecific
Obtain a user-level thread-specific data
associated with a key.
Parameters:
key - a key to retrieve data.
Returns:
a data previously associated with key via
myth_setspecific, or NULL if no data has
been associated with it.
See Also:
<myth_key_create>, <myth_key_delete>, <myth_setspecific>
*/
void *myth_getspecific(myth_key_t key);
/*
Function: myth_get_worker_num
Returns:
The index of the calling thread, an
integer x satisfying
0 <= x < myth_get_num_workers().
See Also:
<myth_get_num_workers>
*/
int myth_get_worker_num(void);
/*
Function: myth_get_num_workers
Returns:
The number of underlying workers.
See Also:
<myth_get_worker_num>
*/
int myth_get_num_workers(void);
typedef pthread_key_t myth_wls_key_t;
/*
Function: myth_wls_key_create
Create a key for worker-specific data.
Parameters:
key - a pointer to which the created key will be stored.
destr_function - a pointer to a destructor function.
wls_key is used to create data specific to each underlying
worker. you can think of it as a simple wrapper to pthread_key_create.
Returns:
Zero if succeed, or an errno when an error occurred.
Bug:
destr_function is ignored in the current implementation.
See Also:
<myth_wls_key_delete>, <myth_wls_setspecific>, <myth_wls_getspecific>
*/
int myth_wls_key_create(myth_wls_key_t *key, void (*destr_function)(void *));
/*
Function: myth_wls_key_delete
Delete a key for worker-specific data.
Parameters:
key - key to delete
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_wls_key_create>, <myth_wls_setspecific>, <myth_wls_getspecific>
*/
int myth_wls_key_delete(myth_wls_key_t key);
/*
Function: myth_wls_setspecific
Associate a worker-specific data with a key.
Parameters:
key - a key created by myth_key_create
data - a data to be associated with key
Returns:
Zero if succeed, or an errno when an error occurred.
See Also:
<myth_wls_key_create>, <myth_wls_key_delete>, <myth_wls_getspecific>
*/
int myth_wls_setspecific(myth_wls_key_t key, const void *data);
/*
Function: myth_wls_getspecific
Obtain a worker-specific data
associated with a key.
Parameters:
key - a key to retrieve data.
Returns:
a data previously associated with key via
myth_wls_setspecific, or NULL if no data has
been associated with it.
See Also:
<myth_wls_key_create>, <myth_wls_key_delete>, <myth_wls_setspecific>
*/
void *myth_wls_getspecific(myth_wls_key_t key);
/*
Function: myth_sched_yield
*/
int myth_sched_yield(void);
/*
Function: myth_sleep
*/
unsigned int myth_sleep(unsigned int s);
/*
Function: myth_usleep
*/
int myth_usleep(useconds_t usec);
/*
Function: myth_nanosleep
*/
int myth_nanosleep(const struct timespec *req, struct timespec *rem);
/*
declare myth_pickle_t to be an opaque structure
*/
typedef struct myth_pickle myth_pickle_t;
void myth_serialize(myth_thread_t th,myth_pickle_t * p);
#define myth_ext_serialize(th,p) myth_serialize(th,p)
myth_thread_t myth_deserialize(myth_pickle_t * p);
myth_thread_t myth_ext_deserialize(myth_pickle_t * p);
myth_thread_t myth_steal(void);
#define myth_ext_steal() myth_steal()
void myth_import(myth_thread_t th);
void myth_ext_import(myth_thread_t th);
void myth_release_stack(myth_thread_t th);
void myth_release_desc(myth_thread_t th);
void myth_log_start(void);
void myth_log_pause(void);
void myth_log_flush(void);
void myth_log_reset(void);
void myth_log_annotate_thread(myth_thread_t th,char *name);
//void myth_log_get_thread_annotation(myth_thread_t th,char *name);
void myth_sched_prof_start(void);
void myth_sched_prof_pause(void);
typedef int (*myth_wsapi_decidefn_t)(myth_thread_t th,void *udata);
myth_thread_t myth_wsapi_runqueue_peek(int victim,void *ptr,size_t *psize);
size_t myth_wsapi_get_hint_size(myth_thread_t th);
void *myth_wsapi_get_hint_ptr(myth_thread_t th);
void myth_wsapi_set_hint(myth_thread_t th,void **data,size_t *size);
int myth_wsapi_rand(void);
void myth_wsapi_randarr(int *ret,int n);
myth_thread_t myth_wsapi_runqueue_take(int victim,myth_wsapi_decidefn_t decidefn,void *udata);
int myth_wsapi_runqueue_pass(int target,myth_thread_t th);
void myth_wsapi_runqueue_push(myth_thread_t th);
myth_thread_t myth_wsapi_runqueue_pop(void);
int myth_wsapi_rand(void);
int myth_wsapi_rand2(int min,int max);
/* TODO: a duplicated definition is in myth_worker.h */
typedef struct myth_thread* (*myth_steal_func_t)(int);
myth_steal_func_t myth_wsapi_set_stealfunc(myth_steal_func_t fn);
#ifdef __cplusplus
} //extern "C"
#endif
#endif /* MYTH_H_ */
|