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