fiber.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #define LIBCO_C
  2. #include "libco.h"
  3. #include "settings.h"
  4. #define WINVER 0x0400
  5. #define _WIN32_WINNT 0x0400
  6. #include <windows.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. static thread_local cothread_t co_active_ = 0;
  11. static void __stdcall co_thunk(void* coentry) {
  12. ((void (*)(void))coentry)();
  13. }
  14. cothread_t co_active() {
  15. if(!co_active_) {
  16. ConvertThreadToFiber(0);
  17. co_active_ = GetCurrentFiber();
  18. }
  19. return co_active_;
  20. }
  21. cothread_t co_derive(void* memory, unsigned int heapsize, void (*coentry)(void)) {
  22. /* Windows fibers do not allow users to supply their own memory */
  23. return (cothread_t)0;
  24. }
  25. cothread_t co_create(unsigned int heapsize, void (*coentry)(void)) {
  26. if(!co_active_) {
  27. ConvertThreadToFiber(0);
  28. co_active_ = GetCurrentFiber();
  29. }
  30. return (cothread_t)CreateFiber(heapsize, co_thunk, (void*)coentry);
  31. }
  32. void co_delete(cothread_t cothread) {
  33. DeleteFiber(cothread);
  34. }
  35. void co_switch(cothread_t cothread) {
  36. co_active_ = cothread;
  37. SwitchToFiber(cothread);
  38. }
  39. int co_serializable() {
  40. return 0;
  41. }
  42. #ifdef __cplusplus
  43. }
  44. #endif