_info 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * antithread - Accelerated Native Technology Implementation of "threads"
  6. *
  7. * On systems with multiple CPUs, it's often faster to split work across
  8. * different execution units. Under Unix-like systems, the two methods of
  9. * doing this are (POSIX) threads or processes.
  10. *
  11. * Threads have the disadvantage that they share all of the address space:
  12. * using software instead of hardware isolation (eg. for malloc) is
  13. * inefficient and less secure. Various subtle errors can occur because
  14. * programmers in one part of the code do not expect concurrency.
  15. *
  16. * Processes have the disadvantage that there is no common infrastructure
  17. * for sharing memory: without this, programmers are faced with the unpalatable
  18. * options of using slower options or creating their own infrastructure.
  19. *
  20. * The antithread module provides memory-sharing infrastructure: the programmer
  21. * indicates the size of the memory to share, and then creates subprocesses
  22. * which share the memory. Pipes are used to hand pointers between the
  23. * main process and the children: usually pointers into the shared memory.
  24. *
  25. * Example:
  26. * #include <ccan/antithread/antithread.h>
  27. * #include <ccan/talloc/talloc.h>
  28. * #include <ctype.h>
  29. * #include <stdlib.h>
  30. * #include <stdio.h>
  31. * #include <string.h>
  32. *
  33. * // Silly example: child makes rot13 copy.
  34. * static void *rot13(struct at_pool *pool, void *unused)
  35. * {
  36. * char *r, *p;
  37. * while ((r = at_read_parent(pool)) != NULL) {
  38. * unsigned int i;
  39. * // r is inside pool, so talloc off it is also inside.
  40. * p = talloc_array(r, char, strlen(r) + 1);
  41. * for (i = 0; r[i]; i++) {
  42. * if (!isalpha(r[i]))
  43. * p[i] = r[i];
  44. * else if (toupper(r[i]) < 'N')
  45. * p[i] = r[i] + 13;
  46. * else
  47. * p[i] = r[i] - 13;
  48. * }
  49. * // Tell parent about our copy.
  50. * at_tell_parent(pool, p);
  51. * }
  52. * return NULL;
  53. * }
  54. *
  55. * #define NUM_CHILDREN 4
  56. *
  57. * int main(int argc, char *argv[])
  58. * {
  59. * struct at_pool *pool;
  60. * struct athread *child[NUM_CHILDREN];
  61. * unsigned int i;
  62. *
  63. * // Create pool and some children
  64. * pool = at_pool(1024*1024);
  65. * for (i = 0; i < NUM_CHILDREN; i++)
  66. * child[i] = at_run(pool, rot13, NULL);
  67. *
  68. * // Pass out work to children.
  69. * for (i = 1; i < argc; i++)
  70. * at_tell(child[i % NUM_CHILDREN],
  71. * talloc_strdup(at_pool_ctx(pool), argv[i]));
  72. *
  73. * // Read back results.
  74. * for (i = 1; i < argc; i++)
  75. * printf("%s ", (char *)at_read(child[i % NUM_CHILDREN]));
  76. * printf("\n");
  77. *
  78. * // Freeing pool kills children, too.
  79. * talloc_free(pool);
  80. * return 0;
  81. * }
  82. *
  83. * License: GPL (3 or any later version)
  84. * Author: Rusty Russell <rusty@rustcorp.com.au>
  85. */
  86. int main(int argc, char *argv[])
  87. {
  88. if (argc != 2)
  89. return 1;
  90. if (strcmp(argv[1], "depends") == 0) {
  91. printf("ccan/talloc\n");
  92. printf("ccan/alloc\n");
  93. printf("ccan/noerr\n");
  94. printf("ccan/read_write_all\n"); /* For tests */
  95. return 0;
  96. }
  97. return 1;
  98. }