Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Can you please explain that why it needs "__sync_synchronize()" in the source code? #1

Closed

Description

@wilsonwen

As I see, there are two ways of using "__sync_synchronize" thi the cstring project.
First, used at the beginning of grab/release api function;
Second, used after calling cstring_clone.
I can't figure out why cstring need this memory barrier here.

Activity

  1. cloudwu commented on Sep 24, 2013

    @cloudwu
    Owner

    I guess memory barrier between cstring s and s->type is needed. maybe it's unusefu. Should I remove then?

  2. wilsonwen commented on Sep 24, 2013

    @wilsonwen
    Author

    I think here's not the situation '__sync_synchronize' needed. No memory ordering need to be guaranteed because you have used lock during interning cstring, which is the only share data in multithread. No where else can change s->type.
    Anyway it affects little, removing it perhaps improve performance a little.

  3. cloudwu commented on Sep 24, 2013

    @cloudwu
    Owner

    I mean

    cstring 
    cstring_persist(const char * cstr, size_t sz) {
        cstring s = cstring_clone(cstr, sz);
        if (s->type == 0) {
            s->type = CSTRING_PERMANENT;
            s->ref = 0;
        }
        __sync_synchronize();
        return s;
    }
  4. wilsonwen commented on Sep 24, 2013

    @wilsonwen
    Author

    Do you worried about "return s" runs before "if (s->type == 0)" or "cstring_clone"?

    But I can't tell the problem it cause without '__sync_synchronize();'.

  5. cloudwu commented on Sep 24, 2013

    @cloudwu
    Owner

    yeah, maybe s was read by flowing cstring_grab() before s->type changed.

    I'm not sure. But I guess I can remove these '__sync_synchronize();'

  6. added a commit that references this issue on Sep 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions