Leaked source code of windows server 2003
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.

129 lines
2.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: thread.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <afx.h>
  11. #include <string.h> /* String support. */
  12. #ifndef __STDC__
  13. #define __STDC__ 1
  14. #endif
  15. extern "C" {
  16. #include "scext.h"
  17. #include "tclhelp.h"
  18. }
  19. #include "tclRdCmd.h"
  20. typedef struct {
  21. Tcl_Interp *interp;
  22. LPCSTR szCmd;
  23. } ProcData;
  24. static DWORD WINAPI
  25. SubCommand(
  26. LPVOID lpParameter)
  27. {
  28. ProcData *pprc = (ProcData *)lpParameter;
  29. return Tcl_Eval(pprc->interp, const_cast<LPSTR>(pprc->szCmd));
  30. }
  31. int
  32. TclExt_threadCmd(
  33. ClientData clientData,
  34. Tcl_Interp *interp,
  35. int argc,
  36. char *argv[])
  37. /*
  38. *
  39. * Function description:
  40. *
  41. * This is the main entry point for the Tcl thread command.
  42. *
  43. *
  44. * Arguments:
  45. *
  46. * ClientData - Ignored.
  47. *
  48. * interp - The Tcl interpreter in force.
  49. *
  50. * argc - The number of arguments received.
  51. *
  52. * argv - The array of actual arguments.
  53. *
  54. *
  55. * Return value:
  56. *
  57. * TCL_OK - All went well
  58. * TCL_ERROR - An error was encountered, details in the return string.
  59. *
  60. *
  61. * Side effects:
  62. *
  63. * None.
  64. *
  65. */
  66. {
  67. CTclCommand tclCmd(interp, argc, argv);
  68. int nTclStatus = TCL_OK;
  69. /*
  70. * thread <commands>
  71. */
  72. try
  73. {
  74. CString szCommand;
  75. HANDLE hThread;
  76. DWORD dwThreadId;
  77. BOOL fSts;
  78. DWORD dwStatus;
  79. ProcData prcData;
  80. tclCmd.NextArgument(szCommand);
  81. tclCmd.NoMoreArguments();
  82. prcData.interp = tclCmd;
  83. prcData.szCmd = szCommand;
  84. /*
  85. * Execute the command in an alternate thread.
  86. */
  87. hThread = CreateThread(
  88. NULL, // pointer to security attributes
  89. 0, // initial thread stack size
  90. SubCommand, // pointer to thread function
  91. &prcData, // argument for new thread
  92. 0, // creation flags
  93. &dwThreadId); // pointer to receive thread ID
  94. if (NULL != hThread)
  95. {
  96. dwStatus = WaitForSingleObject(hThread, INFINITE);
  97. fSts = GetExitCodeThread(hThread, &dwStatus);
  98. CloseHandle(hThread);
  99. nTclStatus = (int)dwStatus;
  100. }
  101. else
  102. {
  103. dwStatus = GetLastError();
  104. tclCmd.SetError(TEXT("Can't create thread: "), dwStatus);
  105. throw dwStatus;
  106. }
  107. }
  108. catch (DWORD)
  109. {
  110. nTclStatus = TCL_ERROR;
  111. }
  112. return nTclStatus;
  113. } /* end TclExt_threadCmd */
  114. /* end thread.cpp */