Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created December 9, 2014 17:52
Show Gist options
  • Save ironpythonbot/3ae570bdede223de9655 to your computer and use it in GitHub Desktop.
Save ironpythonbot/3ae570bdede223de9655 to your computer and use it in GitHub Desktop.
CodePlex Issue #30228 Plain Text Attachments
Index: _struct.cs
===================================================================
--- _struct.cs (revision 16090)
+++ _struct.cs (working copy)
@@ -95,7 +95,11 @@
_formatString = fmt;
Struct s;
- if (_cache.TryGetValue(_formatString, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(_formatString, out s);
+ }
+ if (cached) {
Initialize(s);
} else {
Compile(context, fmt);
@@ -304,7 +308,7 @@
for (int j = 0; j < curFormat.Count; j++) {
res.Add(BigIntegerOps.__int__((BigInteger)CreateUIntValue(context, ref curIndex, _isLittleEndian, data)));
}
- break;
+ break;
case FormatType.Pointer:
for (int j = 0; j < curFormat.Count; j++) {
if (IntPtr.Size == 4) {
@@ -537,7 +541,9 @@
_encodingSize += GetNativeSize(_formats[i].Type) * _formats[i].Count;
}
- _cache.Add(fmt, this);
+ lock (_lockObj) {
+ _cache.Add(fmt, this);
+ }
}
#endregion
@@ -639,17 +645,24 @@
#region Cache of compiled struct patterns
private const int MAX_CACHE_SIZE = 1024;
+ private readonly static object _lockObj = new object();
private static CacheDict<string, Struct> _cache = new CacheDict<string, Struct>(MAX_CACHE_SIZE);
[Documentation("Clear the internal cache.")]
public static void _clearcache() {
- _cache = new CacheDict<string, Struct>(MAX_CACHE_SIZE);
+ lock (_lockObj) {
+ _cache = new CacheDict<string, Struct>(MAX_CACHE_SIZE);
+ }
}
[Documentation("int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible. A floating point\nargument will be truncated towards zero (this does not include a string\nrepresentation of a floating point number!) When converting a string, use\nthe optional base. It is an error to supply a base when converting a\nnon-string. If base is zero, the proper base is guessed based on the\nstring content. If the argument is outside the integer range a\nlong object will be returned instead.")]
public static int calcsize(CodeContext/*!*/ context, [NotNull]string fmt) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.size;
@@ -658,7 +671,11 @@
[Documentation("Return string containing values v1, v2, ... packed according to fmt.")]
public static string/*!*/ pack(CodeContext/*!*/ context, [NotNull]string fmt/*!*/, params object[] values) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.pack(context, values);
@@ -667,7 +684,11 @@
[Documentation("Pack the values v1, v2, ... according to fmt.\nWrite the packed bytes into the writable buffer buf starting at offset.")]
public static void pack_into(CodeContext/*!*/ context, [NotNull]string/*!*/ fmt, [NotNull]ArrayModule.array/*!*/ buffer, int offset, params object[] args) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
s.pack_into(context, buffer, offset, args);
@@ -676,7 +697,11 @@
[Documentation("Unpack the string containing packed C structure data, according to fmt.\nRequires len(string) == calcsize(fmt).")]
public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [NotNull]string/*!*/ fmt, [NotNull]string/*!*/ @string) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack(context, @string);
@@ -685,7 +710,11 @@
[Documentation("Unpack the string containing packed C structure data, according to fmt.\nRequires len(string) == calcsize(fmt).")]
public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [NotNull]string/*!*/ fmt, [NotNull]ArrayModule.array/*!*/ buffer) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack(context, buffer);
@@ -694,7 +723,11 @@
[Documentation("Unpack the string containing packed C structure data, according to fmt.\nRequires len(string) == calcsize(fmt).")]
public static PythonTuple/*!*/ unpack(CodeContext/*!*/ context, [NotNull]string fmt/*!*/, [NotNull]PythonBuffer/*!*/ buffer) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack(context, buffer);
@@ -703,7 +736,11 @@
[Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [NotNull]string fmt/*!*/, [NotNull]ArrayModule.array/*!*/ buffer, [DefaultParameterValue(0)] int offset) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack_from(context, buffer, offset);
@@ -712,7 +749,11 @@
[Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [NotNull]string fmt/*!*/, [NotNull]string/*!*/ buffer, [DefaultParameterValue(0)] int offset) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack_from(context, buffer, offset);
@@ -721,7 +762,11 @@
[Documentation("Unpack the buffer, containing packed C structure data, according to\nfmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).")]
public static PythonTuple/*!*/ unpack_from(CodeContext/*!*/ context, [NotNull]string fmt/*!*/, [NotNull]PythonBuffer/*!*/ buffer, [DefaultParameterValue(0)] int offset) {
Struct s;
- if (!_cache.TryGetValue(fmt, out s)) {
+ bool cached;
+ lock (_lockObj) {
+ cached = _cache.TryGetValue(fmt, out s);
+ }
+ if (!cached) {
s = new Struct(context, fmt);
}
return s.unpack_from(context, buffer, offset);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment