Tls.hx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.thread;
  23. #if doc_gen
  24. @:coreApi
  25. extern class Tls<T> {
  26. public var value(get, set):T;
  27. public function new():Void;
  28. }
  29. #else
  30. /**
  31. Creates thread local storage.
  32. Warning : ATM Tls does not protect the value from being GC'ed. Keep the value reachable to avoid crashes.
  33. */
  34. @:hlNative("std")
  35. abstract Tls<T>(hl.Abstract<"hl_tls">) {
  36. public var value(get, set):T;
  37. /**
  38. Creates thread local storage. This is placeholder that can store
  39. a value that will be different depending on the local thread.
  40. Set the tls value to `null` before exiting the thread
  41. or the memory will never be collected.
  42. **/
  43. public function new() {
  44. this = tls_alloc(true);
  45. }
  46. /**
  47. Returns the value set by tls_set for the local thread.
  48. **/
  49. function get_value():T {
  50. return tls_get(this);
  51. }
  52. /**
  53. Set the value of the TLS for the local thread.
  54. **/
  55. function set_value(v:T) {
  56. tls_set(this, v);
  57. return v;
  58. }
  59. static function tls_alloc(gcValue:Bool)
  60. return null;
  61. static function tls_get(t):Dynamic
  62. return null;
  63. static function tls_set(t, v:Dynamic) {}
  64. }
  65. #end