def encode_str(s, mutable=False):
    """Encodes a SemaphoreStr"""
    rv = ffi.new("SemaphoreStr *")
    if isinstance(s, text_type):
        s = s.encode("utf-8")
    if mutable:
        s = bytearray(s)
    rv.data = ffi.from_buffer(s)
    rv.len = len(s)
    # we have to hold a weak reference here to ensure our string does not
    # get collected before the string is used.
    attached_refs[rv] = s
    return rv
Encodes a SemaphoreStr