[RFC/PATCH] attr: Document a new possible thread safe API
From: Stefan Beller <hidden>
Date: 2016-10-04 22:14:45
Subsystem:
documentation, the rest · Maintainers:
Jonathan Corbet, Linus Torvalds
This is what we want to see at the end of the refactoring session to enable the attr subsystem to be thread safe. Signed-off-by: Stefan Beller <redacted> --- Junio wrote:
quoted
So how would we go about git_all_attrs then?I think you arrived the same conclusion, but ...
I think the changes as proposed are minimal to be able to use attrs in a multithreaded environment. This patch applies on top of jc/attr-more. A complete diff between origin/master..HEAD for the documentation is appended below. Thanks, Stefan Documentation/technical/api-gitattributes.txt | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt
index 92fc32a..940617e 100644
--- a/Documentation/technical/api-gitattributes.txt
+++ b/Documentation/technical/api-gitattributes.txt@@ -59,7 +59,10 @@ Querying Specific Attributes empty `struct git_attr_check` can be prepared by calling `git_attr_check_alloc()` function and then attributes you want to ask about can be added to it with `git_attr_check_append()` - function. + function. git_attr_check_initl is thread safe, i.e. you can call it + from different threads at the same time; internally however only one + call at a time is processed. If the calls from different threads have + the same arguments, the returned `git_attr_check` may be the same. * Call `git_check_attr()` to check the attributes for the path.
@@ -89,15 +92,21 @@ static void setup_check(void) ------------ const char *path; + struct git_attr_check *result; setup_check(); - git_check_attr(path, check); + result = git_check_attr(path, check); ------------ -. Act on `.value` member of the result, left in `check->check[]`: +The result may be the same as `check` (in a single threaded application), +but generally assume it is not. The `result` must not be free'd as it is +owned by the attr subsystem. It is reused by the same thread, so a subsequent +call to git_check_attr in the same thread will overwrite the result. + +. Act on `.value` member of the `result->check[]`: ------------ - const char *value = check->check[0].value; + const char *value = result->check[0].value; if (ATTR_TRUE(value)) { The attribute is Set, by listing only the name of the
@@ -138,10 +147,7 @@ Querying All Attributes To get the values of all attributes associated with a file: -* Prepare an empty `git_attr_check` structure by calling - `git_attr_check_alloc()`. - -* Call `git_all_attrs()`, which populates the `git_attr_check` +* Call `git_all_attrs()`, which returns a `git_attr_check` with the attributes attached to the path. * Iterate over the `git_attr_check.check[]` array to examine
--
2.10.0.129.g35f6318
The following is `git diff origin/master Documentation/technical/api-gitattributes.txt`:
diff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt
index 2602668..940617e 100644
--- a/Documentation/technical/api-gitattributes.txt
+++ b/Documentation/technical/api-gitattributes.txt
@@ -16,10 +16,15 @@ Data Structure
of no interest to the calling programs. The name of the
attribute can be retrieved by calling `git_attr_name()`.
+`struct git_attr_check_elem`::
+
+ This structure represents one attribute and its value.
+
`struct git_attr_check`::
- This structure represents a set of attributes to check in a call
- to `git_check_attr()` function, and receives the results.
+ This structure represents a collection of `git_attr_check_elem`.
+ It is passed to `git_check_attr()` function, specifying the
+ attributes to check, and receives their values.
Attribute Values
@@ -48,49 +53,60 @@ value of the attribute for the path.
Querying Specific Attributes
----------------------------
-* Prepare an array of `struct git_attr_check` to define the list of
- attributes you would want to check. To populate this array, you would
- need to define necessary attributes by calling `git_attr()` function.
+* Prepare `struct git_attr_check` using git_attr_check_initl()
+ function, enumerating the names of attributes whose values you are
+ interested in, terminated with a NULL pointer. Alternatively, an
+ empty `struct git_attr_check` can be prepared by calling
+ `git_attr_check_alloc()` function and then attributes you want to
+ ask about can be added to it with `git_attr_check_append()`
+ function. git_attr_check_initl is thread safe, i.e. you can call it
+ from different threads at the same time; internally however only one
+ call at a time is processed. If the calls from different threads have
+ the same arguments, the returned `git_attr_check` may be the same.
* Call `git_check_attr()` to check the attributes for the path.
-* Inspect `git_attr_check` structure to see how each of the attribute in
- the array is defined for the path.
+* Inspect `git_attr_check` structure to see how each of the
+ attribute in the array is defined for the path.
Example
-------
-To see how attributes "crlf" and "indent" are set for different paths.
+To see how attributes "crlf" and "ident" are set for different paths.
-. Prepare an array of `struct git_attr_check` with two elements (because
- we are checking two attributes). Initialize their `attr` member with
- pointers to `struct git_attr` obtained by calling `git_attr()`:
+. Prepare a `struct git_attr_check` with two elements (because
+ we are checking two attributes):
------------
-static struct git_attr_check check[2];
+static struct git_attr_check *check;
static void setup_check(void)
{
- if (check[0].attr)
+ if (check)
return; /* already done */
- check[0].attr = git_attr("crlf");
- check[1].attr = git_attr("ident");
+ check = git_attr_check_initl("crlf", "ident", NULL);
}
------------
-. Call `git_check_attr()` with the prepared array of `struct git_attr_check`:
+. Call `git_check_attr()` with the prepared `struct git_attr_check`:
------------
const char *path;
+ struct git_attr_check *result;
setup_check();
- git_check_attr(path, ARRAY_SIZE(check), check);
+ result = git_check_attr(path, check);
------------
-. Act on `.value` member of the result, left in `check[]`:
+The result may be the same as `check` (in a single threaded application),
+but generally assume it is not. The `result` must not be free'd as it is
+owned by the attr subsystem. It is reused by the same thread, so a subsequent
+call to git_check_attr in the same thread will overwrite the result.
+
+. Act on `.value` member of the `result->check[]`:
------------
- const char *value = check[0].value;
+ const char *value = result->check[0].value;
if (ATTR_TRUE(value)) {
The attribute is Set, by listing only the name of the
@@ -109,20 +125,36 @@ static void setup_check(void)
}
------------
+To see how attributes in argv[] are set for different paths, only
+the first step in the above would be different.
+
+------------
+static struct git_attr_check *check;
+static void setup_check(const char **argv)
+{
+ check = git_attr_check_alloc();
+ while (*argv) {
+ struct git_attr *attr = git_attr(*argv);
+ git_attr_check_append(check, attr);
+ argv++;
+ }
+}
+------------
+
Querying All Attributes
-----------------------
To get the values of all attributes associated with a file:
-* Call `git_all_attrs()`, which returns an array of `git_attr_check`
- structures.
+* Call `git_all_attrs()`, which returns a `git_attr_check`
+ with the attributes attached to the path.
-* Iterate over the `git_attr_check` array to examine the attribute
- names and values. The name of the attribute described by a
- `git_attr_check` object can be retrieved via
- `git_attr_name(check[i].attr)`. (Please note that no items will be
- returned for unset attributes, so `ATTR_UNSET()` will return false
- for all returned `git_array_check` objects.)
+* Iterate over the `git_attr_check.check[]` array to examine
+ the attribute names and values. The name of the attribute
+ described by a `git_attr_check.check[]` object can be retrieved via
+ `git_attr_name(check->check[i].attr)`. (Please note that no items
+ will be returned for unset attributes, so `ATTR_UNSET()` will return
+ false for all returned `git_array_check` objects.)
-* Free the `git_array_check` array.
+* Free the `git_array_check` by calling `git_attr_check_free()`.