Counter Strike : Global Offensive Source Code
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.

86 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef VMPI_DISTRIBUTE_WORK_H
  7. #define VMPI_DISTRIBUTE_WORK_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "messbuf.h"
  12. #include "utlvector.h"
  13. class IWorkUnitDistributorCallbacks
  14. {
  15. public:
  16. // Called every 200ms or so as it does the work.
  17. // Return true to stop distributing work.
  18. virtual bool Update() { return false; }
  19. // Called when a subsequent number of work units is completed.
  20. // e.g. results received in the following order will trigger
  21. // the following calls to OnWorkUnitsCompleted:
  22. // Work unit numbers: wu2 wu4 wu5 wu1 wu0 wu6 wu3
  23. // Calling OnWorkUnitsCompleted with arg: - - - - 3 - 7
  24. // because when wu0 is received we already have { wu0, wu1, wu2 } so we signal
  25. // that 3 subsequent work units completed, like wise by the time when wu3 is
  26. // received we already have a full set { wu0, wu1, wu2, wu3, wu4, wu5, wu6 }
  27. // and signal that 7 work units completed.
  28. virtual void OnWorkUnitsCompleted( uint64 numWorkUnits ) { return; }
  29. };
  30. // DistributeWork owns this packet ID.
  31. #define VMPI_DISTRIBUTEWORK_PACKETID 2
  32. enum EWorkUnitDistributor
  33. {
  34. k_eWorkUnitDistributor_Default,
  35. k_eWorkUnitDistributor_SDK
  36. };
  37. // Tells which work unit distributor is going to be used.
  38. EWorkUnitDistributor VMPI_GetActiveWorkUnitDistributor();
  39. // Before calling DistributeWork, you can set this and it'll call your virtual functions.
  40. extern IWorkUnitDistributorCallbacks *g_pDistributeWorkCallbacks;
  41. // You must append data to pBuf with the work unit results.
  42. // Note: pBuf will be NULL if this is a local thread doing work on the master.
  43. typedef void (*ProcessWorkUnitFn)( int iThread, uint64 iWorkUnit, MessageBuffer *pBuf );
  44. // pBuf is ready to read the results written to the buffer in ProcessWorkUnitFn.
  45. typedef void (*ReceiveWorkUnitFn)( uint64 iWorkUnit, MessageBuffer *pBuf, int iWorker );
  46. // This is the function vrad and vvis use to divide the work units and send them out.
  47. // It maintains a sliding window of work units so it can always keep the clients busy.
  48. //
  49. // The workers implement processFn to do the job work in a work unit.
  50. // This function must send back a packet formatted with:
  51. // cPacketID (char), cSubPacketID (char), iWorkUnit (int), (app-specific data for the results)
  52. //
  53. // The masters implement receiveFn to receive a work unit's results.
  54. //
  55. // Returns time it took to finish the work.
  56. double DistributeWork(
  57. uint64 nWorkUnits, // how many work units to dole out
  58. ProcessWorkUnitFn processFn, // workers implement this to process a work unit and send results back
  59. ReceiveWorkUnitFn receiveFn // the master implements this to receive a work unit
  60. );
  61. // VMPI calls this before shutting down because any threads that DistributeWork has running must stop,
  62. // otherwise it can crash if a thread tries to send data in the middle of shutting down.
  63. void DistributeWork_Cancel();
  64. #endif // VMPI_DISTRIBUTE_WORK_H