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.

69 lines
1.7 KiB

  1. /*
  2. * compile with :
  3. * $ gcc -Wall -D_GNU_SOURCE -fpic -shared -o pinthread.so pinthread.c -ldl -lpthread
  4. *
  5. * and test it with :
  6. * LD_PRELOAD=/path/to/pinthread.so something
  7. */
  8. //#include <sys/types.h>
  9. //#include <unistd.h>
  10. #include <stdio.h>
  11. #include <pthread.h>
  12. #include <dlfcn.h>
  13. #include <sched.h> //cpu_set_t , CPU_SET
  14. /*
  15. typedef struct {
  16. void *arg;
  17. void *(*fn)(void *);
  18. } real_args;
  19. void *create_head(void *arg)
  20. {
  21. real_args *a = arg;
  22. //fprintf(stderr,"PID %d TID %d\n",
  23. // getpid(),(int)pthread_self());
  24. return a->fn(a->arg);
  25. }
  26. */
  27. static int (*real_pthread_create)(pthread_t *thread,
  28. const pthread_attr_t *attr,
  29. void *(*start_routine) (void *), void *arg);
  30. int pthread_create(pthread_t *thread,
  31. const pthread_attr_t *attr,
  32. void *(*start_routine) (void *), void *arg)
  33. {
  34. //real_args targs;
  35. char *msg;
  36. int ret;
  37. cpu_set_t mask; /* Define your cpu_set bit mask. */
  38. CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
  39. CPU_SET(sched_getcpu(), &mask); /* set the bit that represents core 0. */
  40. //targs.arg = arg;
  41. //targs.fn = start_routine;
  42. if (real_pthread_create == NULL)
  43. {
  44. real_pthread_create = dlsym(RTLD_NEXT,"pthread_create");
  45. if ((msg=dlerror())!=NULL)
  46. printf("**fopen dlopen failed : %s\n", msg);
  47. printf("*wrapping done\n");
  48. }
  49. printf("*about to call original pthread_create\n");
  50. //return real_pthread_create(thread,attr,create_head,&targs);
  51. ret = real_pthread_create(thread,attr,start_routine,arg);
  52. pthread_setaffinity_np(*thread, sizeof(mask), &mask);
  53. return ret;
  54. }