Shared Memory API:

Module shared_atomic

class shared_atomic.atomic_shared_memory. atomic_shared_memory ( initial : bytes = b'\x00' , mode = 'singleprocessing' , length : int = None , paddingdirection : unicode = None , paddingbytes : bytes = b'\x00' , trimming_direction : unicode = 'right' , windows_unix_compatibility : bool = False )

Shared memory provide atomic operations, on Microsoft Windows platform, the size of shared memory should be multiple of 8 bytes!

:attributes

reference: readonly pointer to the start of the shared memory

size: readonly size of the list

mode: readonly ‘s’ for single process, ‘m’ for multiprocessing, on windows platform, only singleprocessing is supported

windows_unix_compatibility: public whether the source code should be compatible with windows x64

constructor to initialize the shared memory

Parameters
  • initial – initial value in the shared memory

  • mode – the mode in which the list will be shared. ‘singleprocessing’ or ‘s’ for single process, ‘multiprocessing’ or ‘m’ for multiprocessing, on windows platform, only singleprocessing is supported, setting it to ‘m’ or ‘multiprocessing’ will be ignored.

  • length – the expected length after padding/trimming for the input value, if not specified, no padding or trimming performed, use original value.

  • paddingdirection – if None, no padding is performed, if specified, right, or left side the padding bytes would be added if not specified, pad to the right side, use ‘right’ or ‘r’ to specify right side, use ‘left’ or ‘l’ to specify the left side.

  • paddingbytes – bytes to pad to the original bytes, by default b’\0’ can be multiple bytes like b’ab’, will be padded to the original bytes in circulation until the expected length is reached.

  • trimming_direction – if initial bytes are longer, on which side the bytes will be trimmed. By default, on the right side, use ‘right’ or ‘r’ to specify right side, use ‘left’ or ‘l’ to specify the left side.

change_mode ( newmode = 'm' )

Switch between singleprocessing mode and multiprocessing mode, the function doesn’t exists on windows, since only single processing mode is supported on windows platform. the contents will be copied , other threads/processes would not be aware of the change.

Parameters

newmode – the mode to change to, ‘m’ or ‘multiprocessing’ for multiproessing, ‘s’ or ‘singleprocessing’ for singleprocessing. default ‘m’

Returns

None

offset_add_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

increment and fetch atomically

Parameters
  • value – data to add

  • offset – the offset inside the shared memory starting from 0 you need to add,

Returns

sum of the 2 values

offset_and_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

bitwise AND and fetch the result atomically

Parameters
  • value – data to AND

  • offset – the offset inside the shared memory starting from 0 you need to AND with,

Returns

resulted value

offset_compare_and_set ( shared_memory2 : self_atomic_shared_memory , value : bytes , offset : size_t = 0 , offset2 : size_t = 0 ) bool

Compare and set atomically. This compares the contents of shared_memory2 at offset2 with the contents of self at offset. If equal, the operation is a read-modify-write operation that writes bytes parameter value into self. If they are not equal, the operation is a read and the current contents of self are written into shared_memory2.

Parameters
  • shared_memory2 – the other shared memory from which the data is from

  • offset – the offset inside the shared memory starting from 0 you need to set,

  • offset2 – the offset2 inside the shared memory 2 starting from 0 you need to get,

Returns

whether the contents of self and contents of shared_memory2 is the same

offset_compare_and_set_value ( i : bytes , n : bytes , offset : size_t = 0 ) bytes

Compare and set atomically. This compares the contents of n with the contents of self at offset. i and n should be at same size and in size of 1,2,4 or 8 bytes. If equal, the operation is a read-modify-write operation that writes bytes parameter value into self. If they are not equal, no operation will be taken.

Parameters
  • i – exchange value

  • n – The value to be compared with

  • offset – the offset inside the shared memory starting from 0 you need to set,

Returns

the original value at offset

offset_compare_with_pointer_and_set ( pointer : _cffi_backend._CDataBase , value : bytes , offset : size_t = 0 ) bool

Compare and set atomically. This compares the contents of pointer with the contents of self at offset. If equal, the operation is a read-modify-write operation that writes bytes parameter value into self. If they are not equal, the operation is a read and the current contents of self are written into pointer.

Parameters
  • pointer – the pointer from which the data is compared with

  • offset – the offset inside the shared memory starting from 0 you need to compare and set,

  • value – value to be set

Returns

whether the contents of self and contents of pointer is the same

offset_fetch_and_add ( value : bytes , offset : size_t = 0 ) bytes

fetch and increment atomically

Parameters
  • value – data to add

  • offset – the offset inside the shared memory starting from 0 you need to add to,

Returns

original value

offset_fetch_and_and ( value : bytes , offset : size_t = 0 ) bytes

fetch then bitwise AND atomically

Parameters
  • value – value to AND to

  • offset – the offset inside the shared memory starting from 0 you need to AND to,

Returns

original value

offset_fetch_and_nand ( value : bytes , offset : size_t = 0 ) bytes

fetch then bitwise NAND atomically

Parameters
  • value – value to NAND with

  • offset – the offset inside the shared memory starting from 0 you need to NAND with,

Returns

original value

offset_fetch_and_or ( value : bytes , offset : size_t = 0 ) bytes

fetch then bitwise OR atomically

Parameters
  • value – value to OR with

  • offset – the offset inside the shared memory starting from 0 you need to OR with,

Returns

original value

offset_fetch_and_sub ( value : bytes , offset : size_t = 0 ) bytes

fetch and substract atomically

Parameters
  • value – data to add

  • offset – the offset inside the shared memory starting from 0 you need to substract from,

Returns

original value

offset_fetch_and_xor ( value : bytes , offset : size_t = 0 ) bytes

fetch then bitwise XOR atomically

Parameters
  • value – value to XOR with

  • offset – the offset inside the shared memory starting from 0 you need to XOR with,

Returns

original value

offset_get ( offset: long long = 0 , length: char = 1 ) bytes

Atomically get the bytes at specific offset given specific length

Parameters
  • offset – the offset inside the shared memory starting from 0

  • length – the length of bytes should be retrieved

Returns

bytes at specific offset.

offset_get_and_set ( value: bytes , offset: long long = 0 ) bytes

Atomically set the bytes at specific offset given specific length

Parameters
  • value – new value in bytes

  • offset – the offset inside the shared memory starting from 0

Returns

bytes at specific offset previously.

offset_memmove ( mv : memoryview , offset : size_t = 0 )

Set the data at specific offset given specific length using memcpy, it’s NOT atomic method the data can be any length incase you don’t need atomicy or should use other lengths :param mv: memoryview: the data source as memoryview to read from :param shared_memory2: the other shared memory from which the data is from :param offset: the offset inside the shared memory starting from 0 you need to set, get the data from the shared memory 2, could be any length :return: None

offset_nand_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

bitwise NAND and fetch the result atomically

Parameters
  • value – data to NAND

  • offset – the offset inside the shared memory starting from 0 you need to NAND with,

Returns

resulted value

offset_or_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

bitwise OR and fetch the result atomically

Parameters
  • value – data to OR

  • offset – the offset inside the shared memory starting from 0 you need to OR with,

Returns

resulted value

offset_store ( shared_memory2 : self_atomic_shared_memory , offset : size_t = 0 , offset2 : size_t = 0 , length : char = 1 )

Atomically set the data at specific offset given specific length

Parameters
  • shared_memory2 – the other shared memory from which the data is from

  • offset – the offset inside the shared memory starting from 0 you need to set,

  • offset2 – the offset inside the shared memory from the data you want,

  • length – the length of bytes should be set in the shared memory and

get the data from the shared memory 2, only 1,2,4 and 8 are supported :return: None

offset_store_from_pointer ( pointer : _cffi_backend._CDataBase , offset : size_t = 0 )

Atomically set the data at specific offset given another pointer

Parameters
  • pointer – the pointer from which the data is from

  • offset – the offset inside the shared memory starting from 0 you need to set

get the data from the pointer :return: None

offset_sub_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

substract and fetch atomically

Parameters
  • value – data to substract

  • offset – the offset inside the shared memory starting from 0 you need to add,

Returns

difference of the 2 values

offset_xor_and_fetch ( value : bytes , offset : size_t = 0 ) bytes

bitwise XOR and fetch the result atomically

Parameters
  • value – data to XOR

  • offset – the offset inside the shared memory starting from 0 you need to XOR with,

Returns

resulted value

set_atomic_methods ( operation_length : char )

change the operation methods based on the operation_length :param operation_length: number of bytes to operate on, should be 1,2,4 or 8 :return: None