async.hh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #ifndef I4_ASYNC_READ_HH
  9. #define I4_ASYNC_READ_HH
  10. // This class should probably only be used by files in "i4/files/*"
  11. // This class is a portable implemented of async file reading
  12. // A thread gets created during init() which runs as long as
  13. // there is stuff to read and then blocks for a signal from the main
  14. // program so if nothing needs reading, it runs efficiently. Request
  15. // to be read are qued up (max of 16). The request are processed
  16. // serially. If you want to read from multiple devices in parallel,
  17. // you should create two i4_async_reads, one for each device.
  18. #include "memory/que.hh"
  19. #include "threads/threads.hh"
  20. #include "init/init.hh"
  21. #include "file/file.hh"
  22. class i4_async_reader : public i4_init_class
  23. {
  24. volatile i4_bool stop;
  25. i4_signal_object sig;
  26. i4_bool emulation;
  27. struct read_request
  28. {
  29. sw32 fd;
  30. w32 size;
  31. void *buffer;
  32. i4_file_class::async_callback callback;
  33. void *context;
  34. read_request(sw32 fd, void *buffer,
  35. w32 size, i4_file_class::async_callback callback,
  36. void *context)
  37. : fd(fd), buffer(buffer), size(size), callback(callback), context(context) {}
  38. read_request() { ; }
  39. };
  40. enum { MAX_REQUEST=16 };
  41. enum { STACK_SIZE=8096 };
  42. i4_critical_section_class que_lock;
  43. i4_fixed_que<read_request, MAX_REQUEST> request_que;
  44. void emulate_speeds(read_request &r);
  45. protected:
  46. virtual w32 read(sw32 fd, void *buffer, w32 count) = 0;
  47. public:
  48. // name is just some unique name. Windows requires this for Semaphores
  49. i4_async_reader(char *name);
  50. void init(); // creates thread (called by i4_init()
  51. void uninit(); // waits for thread to die (called by i4_uninit()
  52. // ques up a request
  53. i4_bool start_read(int fd, void *buffer, w32 size,
  54. i4_file_class::async_callback call,
  55. void *context);
  56. void PRIVATE_thread(); // don't call this!
  57. };
  58. #endif