Team Fortress 2 Source Code as on 22/4/2020
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.

89 lines
3.2 KiB

  1. //========= Copyright 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. enum EWorkUnitDistributor
  31. {
  32. k_eWorkUnitDistributor_Default,
  33. k_eWorkUnitDistributor_SDK
  34. };
  35. // Tells which work unit distributor is going to be used.
  36. EWorkUnitDistributor VMPI_GetActiveWorkUnitDistributor();
  37. // Before calling DistributeWork, you can set this and it'll call your virtual functions.
  38. extern IWorkUnitDistributorCallbacks *g_pDistributeWorkCallbacks;
  39. // You must append data to pBuf with the work unit results.
  40. // Note: pBuf will be NULL if this is a local thread doing work on the master.
  41. typedef void (*ProcessWorkUnitFn)( int iThread, uint64 iWorkUnit, MessageBuffer *pBuf );
  42. // pBuf is ready to read the results written to the buffer in ProcessWorkUnitFn.
  43. typedef void (*ReceiveWorkUnitFn)( uint64 iWorkUnit, MessageBuffer *pBuf, int iWorker );
  44. // Use a CDispatchReg to register this function with whatever packet ID you give to DistributeWork.
  45. bool DistributeWorkDispatch( MessageBuffer *pBuf, int iSource, int iPacketID );
  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. char cPacketID, // This packet ID must be reserved for DistributeWork and DistributeWorkDispatch
  59. // must be registered with it.
  60. ProcessWorkUnitFn processFn, // workers implement this to process a work unit and send results back
  61. ReceiveWorkUnitFn receiveFn // the master implements this to receive a work unit
  62. );
  63. // VMPI calls this before shutting down because any threads that DistributeWork has running must stop,
  64. // otherwise it can crash if a thread tries to send data in the middle of shutting down.
  65. void DistributeWork_Cancel();
  66. #endif // VMPI_DISTRIBUTE_WORK_H